1 / 11

Java and Variables

Java and Variables. Overview. Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage Collection Arrays. Declaring a Variable. Variables can be broken into two groups Primitives Object references

hansel
Télécharger la présentation

Java and Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java and Variables

  2. Overview • Declaring a Variable • Primitive Types • Java Keywords • Reference Variables • Object Declaration and Assignment • Objects and Garbage Collection • Arrays

  3. Declaring a Variable • Variables can be broken into two groups • Primitives • Object references • Primitives contain single values • Object references allow us to touch object instances

  4. Declaring a Variable • Just like in C, Java needs you to set the type of each variable • Primitive types have the same names as in C but different sizes • Java does not have an unsigned type • To put a bigger primitive into a smaller primitive, use typecasting • int i = (int)12.5f;

  5. Primitive Types • boolean – true or false • char – 16 bits • Unicode not ASCII • byte – 8 bits • -128 to 127 • short – 16 bits • -32,768 to 32767 • int – 32 bits • -2,147,483,648 to 2,147,483,647

  6. Java Keywords • Just like C, Java has special words you can not use for variables • Eg. throw, package, break, while, final, default, long, short, super, this, enum, catch, etc...

  7. Reference Variables • When an object is declared, it is a reference to any object not the object itself • A good analogy would be it’s a pointer • You must use the ‘dot’ ( . ) operator when working with and object reference variable • E.g. • myObject.dosomething() • myObject.somevariable

  8. Object Declaration and Assignment • The statement Dog mydog = new Dog(); works in 3 steps: • A new reference variable (mydog) is defined • Space is made in memory for a Dog object • The mydog object reference is set to point to the memory where the Dog object was placed

  9. Objects and Garbage Collection • Since object reference variables only ‘pointer’ to objects, we can change which object they point to easily • If an object does not have at least on object reference variable, it is lost and will get destroyed

  10. Arrays • Arrays are objects in Java, hence • int[] nums; // not enough • nums = new int[7]; // is also needed • Other than the definition, they work remarkably like arrays in C • The first element is ‘0’ • You use [] to access different elements

  11. Simple Example • Using the code on the web, add a 3rd cat to the cat array and print out its information • Add a new piece of information to all cats and print it out also

More Related