Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance...
-
Upload
samantha-simon -
Category
Documents
-
view
225 -
download
4
description
Transcript of Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance...
![Page 1: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/1.jpg)
Variables and TypesJava Part 3
![Page 2: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/2.jpg)
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.
![Page 3: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/3.jpg)
8 Intrinsic (Primitive) Types int 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
![Page 4: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/4.jpg)
Abstract Types String This is a class defined in the
java library and is used to hold strings of characters.
For ex, name=“Joe Smith”
Font used to change the font style of your output
Note: These begin with an uppercase letters – denotes a class name.
![Page 5: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/5.jpg)
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.
![Page 6: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/6.jpg)
Declaring Variablesint 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 user-defined type.
![Page 7: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/7.jpg)
Referencing VariablesIntrinsic 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.
![Page 8: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/8.jpg)
What is a reference?Monster m = new Monster();m
Monster Object
0xF50xF5
m stores the address of a Monster
![Page 9: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/9.jpg)
What is a primitive variable?int numDays = 365;
numDays
365
numDays stores an integer value
![Page 10: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/10.jpg)
Real Numbers
![Page 11: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/11.jpg)
Reals – predict the outputdouble one = 99.57;double two = 3217;float three = 23.32f;
System.out.println(one);System.out.println(two);System.out.println(three);
OUTPUT99.573217.023.32
![Page 12: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/12.jpg)
double one = 120.7; System.out.println(one);one = 125;System.out.println(one);System.out.println((int)one);
OUTPUT120.7125.0125
![Page 13: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/13.jpg)
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;
![Page 14: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/14.jpg)
Ascii ValuesASCII values you MUST know!!!
‘A’ 65‘a’ 97‘0’ 48
char letter = 97; out.println(letter); //outs aletter = 'A';out.println(letter); //outs A
![Page 15: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/15.jpg)
Characterschar 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);
OUTPUTAAC67
![Page 16: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/16.jpg)
Booleans
Booleans can only store true or false
boolean go = true;System.out.println(go);boolean stop = false;System.out.println(stop);
OUTPUTtruefalse
![Page 17: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/17.jpg)
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);
OUTPUThello worldbuddy = whoot - \\\\\\
![Page 18: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/18.jpg)
Variables AssignmentThe 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.
![Page 19: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/19.jpg)
Assignment Practiceint number = 75, bigNum=99;double hTownTax = 8.25;char bigA = 'A', littleA = 'a';boolean isPrime = false;String s = "abc";
System.out.println(number);System.out.println(bigNum);System.out.printf("%.2f\n",hTownTax);System.out.println(bigA);System.out.println(littleA);System.out.println(isPrime);System.out.println(s);
OUTPUT75998.25Aafalseabc
![Page 20: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/20.jpg)
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-128127-3276832767
![Page 21: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/21.jpg)
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;
![Page 22: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/22.jpg)
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()
![Page 23: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/23.jpg)
int one = 90;double dec = 234;char letter = 'A';
System.out.println( one );one = letter; //char to intSystem.out.println( one );
one = 'A'; //char to intSystem.out.println( one );
System.out.println( dec );dec = one; //int to doubleSystem.out.println( dec );
OUTPUT906565234.065.0
Data type sizesoften determine if assignment is legal.32 bit == 32 bit
![Page 24: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/24.jpg)
AutoBoxing and AutoUnboxingIn JAVA, you have 8 primitive data types.
All other variables in Java are referencevariables. References refer to objects.
Monster m = new Monster();
![Page 25: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/25.jpg)
For each of the primitive types, there is a corresponding wrapper class.
primitive objectbyte Byte short Short
int Integer long Long float Float
double Double char Character
boolean Boolean
![Page 26: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/26.jpg)
Box/UnboxInteger numOne = 99;Integer numTwo = new Integer(99);
=99;=new Integer(99);These two lines are equivalent.
![Page 27: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/27.jpg)
Box/UnboxDouble numOne = 99.1;Double numTwo = 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.
![Page 28: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/28.jpg)
GUI Help//gui example
import javax.swing.JOptionPane;
public class GuiHelp { public static void main(String args[]) { { int one, two, total; one = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer :: ")); two = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer :: "));
total = one + two; JOptionPane.showMessageDialog(null,"Total ::" + total); System.out.println(total); }}
![Page 29: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/29.jpg)
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.
![Page 30: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/30.jpg)
Assignment Operators total = total + number; total += number;
numPlayers = numPlayers -3; numPlayers -= 3;
Assignment operators include:= , += , -= , *=, /= , %=
![Page 31: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/31.jpg)
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 fetchPostfix: x++; fetch, then increment
There is a difference…
![Page 32: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/32.jpg)
Concatention The + sign is used to concatenate
strings and/or variable values.
Relational Operators!= not equal to== equal to
![Page 33: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/33.jpg)
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 useds indicates String
![Page 34: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/34.jpg)
NumberFormat Class import java.text.NumberFormat;
money=NumberFormat.getCurrencyInstance();number=NumberFormat.getIntegerInstance();decimal = NumberFormat.getNumberInstance();percent=NumberFormat.getPercentInstance();
![Page 35: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/35.jpg)
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
![Page 36: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/36.jpg)
Scanner Methods next() returns a string (no spaces)
use after reading in numeric data as nextLine() will read in the EOL character, not the next actual line
nextLine() returns string up to EOL nextInt() returns the int from the
stream nextDouble() nextBoolean() close() closes the input stream
![Page 37: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/37.jpg)
![Page 38: Variables and Types Java Part 3. Class Fields Aka member variable, field variable, instance variable, class variable. These are the descriptors of.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ad57f8b9ab059972c16/html5/thumbnails/38.jpg)
Assignments Glencoe book, p 130, #5 – use wordpad Worksheets
Variables Wk 0-2 Assignments from weebly