Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.

Similar presentations


Presentation on theme: "Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of."— Presentation transcript:

1 Variables and Types Java Part 3

2 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.

3 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

4 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.

5 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.

6 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 user- defined type.

7 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.

8 What is a reference? Monster m = new Monster(); m Monster Object 0xF5 m stores the address of a Monster

9 What is a primitive variable? int numDays = 365; numDays 365 numDays stores an integer value

10 Real Numbers

11 Reals – predict the output double one = 99.57; double two = 3217; float three = 23.32f; System.out.println(one); System.out.println(two); System.out.println(three); OUTPUT 99.57 3217.0 23.32

12 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

13 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;

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

15 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 C 67

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

17 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 - \\\\\\

18 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.

19 Assignment Practice int 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); OUTPUT 75 99 8.25 A a false abc

20 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

21 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;

22  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()

23 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 234.0 65.0 Data type sizes often determine if assignment is legal. 32 bit == 32 bit

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

25 For each of the primitive types, there is a corresponding wrapper class. primitiveobject byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean

26 Box/Unbox Integer numOne = 99; Integer numTwo = new Integer(99); =99; =new Integer(99); These two lines are equivalent.

27 Box/Unbox Double 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.

28 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); }

29 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.

30 Assignment Operators  total = total + number;  total += number;  numPlayers = numPlayers -3;  numPlayers -= 3; Assignment operators include: =, +=, -=, *=, /=, %=

31 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…

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

33 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

34 NumberFormat Class  import java.text.NumberFormat; money=NumberFormat.getCurrencyInstance(); number=NumberFormat.getIntegerInstance(); decimal = NumberFormat.getNumberInstance(); percent=NumberFormat.getPercentInstance();

35 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

36 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

37

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


Download ppt "Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of."

Similar presentations


Ads by Google