Variables and Types Java Part 3 Class Fields

Variables and Types Java Part 3

Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of the object. The type associated with these variables can be built in or user-defined.

8 Intrinsic (Primitive) Types integer (4 bytes) double number with a decimal (8 bytes) boolean true / false (2 bytes) char single character (2 bytes) byte (1 byte) short (2 bytes) long (8 bytes) float (4 bytes) Note: these all begin with a lowercase letter

Abstract Types String This is a class defined in the java library and is used to hold strings of characters. Note: These begin with an For ex, name=“Joe Smith” Font uppercase letters – denotes a class name. used to change the font style of your output

User-Defined Types Another type of abstract data You can declare member variables to be of user-defined types, such as any class that you have created. For ex, if you create a Widget class, and save it, you can declare things of type Widget in another project, or this same project.

Declaring Variables int length; int length, width; int length = 10; In the last statement, the variable is declared and initialized in one statement. Monster m = new Monster(); This is how you declare a variable of a userdefined type.

Referencing Variables Intrinsic variables store the value of the variable. Object variables (such as type String, or anything you create) store the reference (address) to the data, not the data itself. This will become important when you are changing the values of an object later on.

What is a reference? Monster m = new Monster(); m 0 x. F 5 Monster Object m stores the address of a Monster

What is a primitive variable? int num. Days = 365; num. Days 365 num. Days stores an integer value

Real Numbers

Reals – predict the output double one = 99. 57; double two = 3217; float three = 23. 32 f; System. out. println(one); System. out. println(two); System. out. println(three); OUTPUT 99. 57 3217. 0 23. 32

double one = 120. 7; System. out. println(one); one = 125; System. out. println(one); System. out. println((int)one); OUTPUT 120. 7 125. 0 125

Characters Character variables are used to store a single letter. Character variables are actually integers. char let = 'A'; char fun = 65; char test = 'a'; char go = 97; char what = 48;

Ascii Values ASCII values you MUST know!!! ‘A’ 65 ‘a’ 97 ‘ 0’ 48 char letter = 97; out. println(letter); letter = 'A'; out. println(letter); //outs a //outs A

Characters char alpha = 'A'; char ascii = 65; char sum = 'B' + 1; System. out. println(alpha); System. out. println(ascii); System. out. println(sum); System. out. println('B'+1); OUTPUT A A C 67

Booleans can only store true or false boolean go = true; System. out. println(go); boolean stop = false; System. out. println(stop); OUTPUT true false

Strings String is a class that stores a group of characters String dude = "hello world"; String buddy = "whoot - \\\"; System. out. println(dude); System. out. println("buddy = " + buddy); OUTPUT hello world buddy = whoot - \\\

Variables Assignment The statement receiver = 57; assigns the value 57 to the variable receiver. It is assumed that the variable receiver has already been declared before this point. int num = 82; declares the variable and assigns it a value in one statement.

Assignment Practice int number = 75, big. Num=99; double h. Town. Tax = 8. 25; char big. A = 'A', little. A = 'a'; boolean is. Prime = false; String s = "abc"; System. out. println(number); System. out. println(big. Num); System. out. printf("%. 2 fn", h. Town. Tax); System. out. println(big. A); System. out. println(little. A); System. out. println(is. Prime); System. out. println(s); OUTPUT 75 99 8. 25 A a false abc

How Big and Small can my variables go? The MIN_VALUE and MAX_VALUE fields store the minimum and maximum values that can be stored in a particular type. System. out. println(Byte. MIN_VALUE); System. out. println(Byte. MAX_VALUE); System. out. println(Short. MIN_VALUE); System. out. println(Short. MAX_VALUE); OUTPUT -128 127 -32768 32767

Mixing Types Java is a stong typed language. You must pay attention to a variable’s type when assigning a value. Integers cannot store decimals unless a cast is used. int one=90; char letter= ‘A’; char let= 97; one=letter; letter=let; one=let;

When assigning values from one variable to another, data types and data type sizes play an important part. Large size types can be assigned same size type and smaller type values. Smaller size types cannot be assigned larger type values. 16 bit = 16 bit and smaller //legal 8 bit = 16 bit and smaller //illegal without a cast()

int one = 90; double dec = 234; char letter = 'A'; System. out. println( one ); one = letter; //char to int System. out. println( one ); one = 'A'; //char to int System. out. println( one ); System. out. println( dec ); dec = one; //int to double System. out. println( dec ); OUTPUT 90 65 65 234. 0 65. 0 Data type sizes often determine if assignment is legal. 32 bit == 32 bit

Auto. Boxing and Auto. Unboxing In JAVA, you have 8 primitive data types. All other variables in Java are reference variables. References refer to objects. Monster m = new Monster();

For each of the primitive types, there is a corresponding wrapper class. primitive object byte Byte short int long float double char boolean Short Integer Long Float Double Character Boolean

Box/Unbox Integer num. One = 99; Integer num. Two = new Integer(99); =99; =new Integer(99); These two lines are equivalent.

Box/Unbox Double num. One = 99. 1; Double num. Two = new Double(99. 1); =99. 1; =new Double(99. 1); These two lines are equivalent. With the introduction of Java 5, the new Double() Object instantiation code happens in the background. Java takes care of these details, but does not show the work.

GUI Help //gui example import javax. swing. JOption. Pane; public class Gui. Help { public static void main(String args[]) { { int one, two, total; one = Integer. parse. Int(JOption. Pane. show. Input. Dialog("Enter an integer : : ")); two = Integer. parse. Int(JOption. Pane. show. Input. Dialog("Enter an integer : : ")); total = one + two; JOption. Pane. show. Message. Dialog(null, "Total : : " + total); System. out. println(total); } }

Constants In java, we refer to constants as finals. A declaration would look like: final double PI = 3. 14; The name of the final would be in UPPERCASE letters. Use finals whenever you have values that don’t change while the program is running.

Assignment Operators total = total + number; total += number; num. Players = num. Players -3; num. Players -= 3; Assignment operators include: = , += , -= , *=, /= , %=

Increment and Decrement The increment operator (++) means add by 1 The decrement operator (--) means decrease by 1 These can be prefix or postfix. Prefix: ++x; increment, then fetch Postfix: x++; fetch, then increment There is a difference…

Concatention The + sign is used to concatenate strings and/or variable values. Relational Operators != not equal to == equal to

format() This can be used to format numeric values. A specifier takes the form: %[alignment][width][. decimal]f where [. decimal] indicates the number of decimal places and f indicates a floating point number. For an integer: %[alignment][width]d % start of specifier [alignment] skip for right alignment, - for left align [width] num of characters used s indicates String

Number. Format Class import java. text. Number. Format; money=Number. Format. get. Currency. Instance(); number=Number. Format. get. Integer. Instance(); decimal = Number. Format. get. Number. Instance(); percent=Number. Format. get. Percent. Instance();

Getting Input from User An input stream is a sequence of characters received from an input device, such as a keyboard. import java. util. Scanner to get input from the keyboard. Use System. in to read in the input

Scanner Methods next() returns a string (no spaces) use after reading in numeric data as next. Line() will read in the EOL character, not the next actual line next. Line() returns string up to EOL next. Int() returns the int from the stream next. Double() next. Boolean() close() closes the input stream


Assignments Glencoe book, p 130, #5 – use wordpad Worksheets Variables Wk 0 -2 Assignments from weebly
- Slides: 38