1 / 10

Unit 6 - Variables - Fundamental Data Types

Unit 6 - Variables - Fundamental Data Types. Variables. A variable is a “named container” that holds a value. Example: q = 100 - q; means: 1. Read the current value of q 2. Subtract it from 100 3. Move the result back into q. mov ax,q mov bx,100

Download Presentation

Unit 6 - Variables - Fundamental Data Types

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. Unit 6- Variables- Fundamental Data Types

  2. Variables • A variable is a “named container” that holds a value. • Example: q = 100 - q; means: • 1. Read the current value of q • 2. Subtract it from 100 • 3. Move the result back into q mov ax,q mov bx,100 sub bx,ax mov q,bx

  3. Fundamental Data Types in Java Type NameKind of dataMemory usedSize range int integers 4 bytes -2,147,483,648 to 2,147,483,647 double decimal #s 8 bytes ±1.76769 x 10^308 to ± 4.94 x 10^-324 char single 2 bytes all characters character boolean true or false 1 bit true or false ALL OTHER TYPES ARE CLASSES!!!

  4. Your first Java class Class NameKind of dataMemory usedSize range String words or varies any word phrases or phrase

  5. Variables (cont’d) • A variable must be declared before it can be used: int count; double x, y; JButton go; Walker amy; String firstName; Name(s) Type The name should be descriptive!!

  6. Variables (cont’d) • The assignment operator = sets the variable’s value: Examples int numberOfStudents; double myGPA = 4.15; boolean studentAbsent = false; String firstName; String phoneNumber = “707-433-5777”;

  7. Variables: Scope • Each variable has a scope — the area in the source code where it is “visible.” • If you use a variable outside its scope, the compiler reports a syntax error. • Variables can have the same name when their scopes do not overlap. { int k = ...; ... } for (int k = ...) { ... }

  8. Variables: Fields public class SomeClass { } Fields Scope Constructors and methods Or: public class SomeClass { } Constructors and methods Scope Fields

  9. Variables: Local Variables • Local variables are declared inside a constructor or a method. • Local variables lose their values and are destroyed once the constructor or the method is exited. • The scope of a local variable is from its declaration down to the closing brace of the block in which it is declared.

  10. Variables: Local Variables public class SomeClass { ... public SomeTypeSomeMethod(...) { { } } ... } Scope Local variable declared Local variable declared

More Related