chapter3 basic java data types and declarations

48
CHAPTER 3 BASIC JAVA DECLARATIONS

Upload: sshhzap

Post on 19-May-2015

2.213 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Chapter3 basic java data types and declarations

CHAPTER 3 BASIC JAVA DECLARATIONS

Page 2: Chapter3 basic java data types and declarations
Page 3: Chapter3 basic java data types and declarations
Page 4: Chapter3 basic java data types and declarations
Page 5: Chapter3 basic java data types and declarations

Constant

• eggsPerBasket = eggsPerBasket – 2;• The number 2 in the preceding assignment

statement is called constant.

Page 6: Chapter3 basic java data types and declarations

REMINDER

• numberOfBaskets = dale.readLineInt()• This is an assignment statement that sets the

value of the variable numberOfBaskets equal to the value returned by the expression

Page 7: Chapter3 basic java data types and declarations

REMINDER

• dale.readLineInt()• A method is an action, and an invocation of a

method causes that action to take place. The action performed by the method readLineInt is to read a single integer from a line of input and deliver that value to the program. In this case, the value becomes the new value of variable numberOfBaskets.

Page 8: Chapter3 basic java data types and declarations
Page 9: Chapter3 basic java data types and declarations
Page 10: Chapter3 basic java data types and declarations

Variable

• USED TO STORE DATA SUCH AS NUMBERS AND LETTERS.

• THE NUMBER , LETTER OR OTHER DATA ITEM IN A VARIABLES IS CALLED VALUE.

Page 11: Chapter3 basic java data types and declarations
Page 12: Chapter3 basic java data types and declarations
Page 13: Chapter3 basic java data types and declarations
Page 14: Chapter3 basic java data types and declarations
Page 15: Chapter3 basic java data types and declarations
Page 16: Chapter3 basic java data types and declarations
Page 17: Chapter3 basic java data types and declarations
Page 18: Chapter3 basic java data types and declarations
Page 19: Chapter3 basic java data types and declarations
Page 20: Chapter3 basic java data types and declarations
Page 21: Chapter3 basic java data types and declarations
Page 22: Chapter3 basic java data types and declarations
Page 23: Chapter3 basic java data types and declarations

TYPE CASTING

• IS THE CHANGING OF THE TYPE OF VALUE FROM ITS NORMAL TYPE TO SOME OTHER TYPE SUCH AS CHANGING 2.0 FROM double to int.

• Points = (int) distance;

Page 24: Chapter3 basic java data types and declarations

REMINDER

• BE SURE TO NOTE THAT WHEN YOU TYPE CAST FROM double to an int( or from any floating-point type to any integer type), the amount is not rounded. The part after the decimal point is simple discarded. This known as truncating.

Page 25: Chapter3 basic java data types and declarations
Page 26: Chapter3 basic java data types and declarations
Page 27: Chapter3 basic java data types and declarations
Page 28: Chapter3 basic java data types and declarations

String

• A String variable is not just a simple variable, like a variable of type int is. A String variable of a class type. Something of a class type is an object with methods as well as a value. A string variable is an object and as an object it has methods. These String methods can be used to manipulate string values.

Page 29: Chapter3 basic java data types and declarations

length()

• Description : returns length of the string object.

• Example : String greeting = “Hello!”;greeting.length() returns 6.

Page 30: Chapter3 basic java data types and declarations

equals(Other_String)

• Returns true if the calling object string and the Other_String are equal, considering uppercase and lowercase versions of a letter to be the same. Otherwise, returns false.

• String greeting = dale.readLine();if (greeting.equals(“Hi”))System.out.println( “Informal Greeting…”);

Page 31: Chapter3 basic java data types and declarations

equalsIgnoreCase(Other_String)

• Returns true if the calling object string and the Other_String are equal, considering uppercase and lowercase versions of a letter to be the same. Otherwise, returns false.

• If a program contains,String s1 = “mary!”;then after this assignments1.equalsIgnoreCase(“Mary!”)returns true

Page 32: Chapter3 basic java data types and declarations

ToLowerCase()

• Returns a string with the same characters as the string object, but with all characters converted to lowercase

• String greeting = “Hi Mary!”;greeting.toLowerCase() returns

“hi mary!”

Page 33: Chapter3 basic java data types and declarations

toUpperCase()

• Returns a string with the same characters as the string object, but with all characters converted to uppercase.

• String greeting = “Hi Mary!”;greeting.toLowerCase() returns

“HI MARY!”

Page 34: Chapter3 basic java data types and declarations

Trim()

• Returns a string with the same characters as the string object, but with leading and trailing white space removed.

• String pause = “ hmm ”;pause.trim()returns “hmm”

Page 35: Chapter3 basic java data types and declarations

charAt(Position)

• Returns the character in the string at the Position. Positions are counted 0,1,2 etc.

String greeting = “Hello!”;greeting.charAt(0) returns ‘H’.greeting.charAt(1) returns ‘e’.

Page 36: Chapter3 basic java data types and declarations

Substring(Start)

• Returns the substring of the string object starting from Start through to the end of the string object. Positions are counted 0,1,2, etc.

• String sample = “abcdefG”;Sample.substring(2) returns “cdefG”.

Page 37: Chapter3 basic java data types and declarations

Substring(Start,End)

• Returns the substring of the string object starting from position Start through but not including, position End of the string object. Positions are counted 0,1,2, etc.

• String sample = “abcdefG”:sample.substring(2, 5) returns“cdefG”.

Page 38: Chapter3 basic java data types and declarations

indexOf(A_String)

• Returns the position of the first occurrence of the string A_String in the string object. Positions are counted 0,1,2, etc. returns -1 if A_String is not found.

• String greeting = “Hi Mary!”;greeting.indexOf(“Mary”)returns 3.greeting.indexOf(“Sally”)returns -1.

Page 39: Chapter3 basic java data types and declarations

indexOf(A_String.Start)• Returns the position of the first occurrence of the string in the string

object that occurs at or after position Start. Positions are counted 0,1,2 etc returns -1 if A_String is not found.

• String name = “Mary. Mary quite contrary”;name.indexOf(“Mary” , 1)returns 6. The same value is returned if 1 is replaced by any number up to including 6.name.indexOf(“Mary”, 0)returns 0.Name.indexOf(“Mary” , 8)returns -1.

Page 40: Chapter3 basic java data types and declarations

LastIndexOf(A_String)

• Returns the positions of the last occurrence of the string A_String in the string object. Positions are counted 0,1,2 etc. returns -1, if A_String is not found.

• String name = “Mary , Mary, Mary quite so”;name.lastIndexOf(“Mary”)returns 12.

Page 41: Chapter3 basic java data types and declarations

compareTo(A_String)• Compares the calling string object and the string object and

the string argument to see which comes first in the lexicographic ordering. Lexicographic ordering is the same as alphabetical ordering when both strings are either all uppercase or all lowercase. If the two strings are equal, it returns zero. If the argument is first, it returns a positive number.

• String entry = “adventure”;entry.compareTo(“zoo”) returns a negative numberentry.compareTo(“Adventure”) returns zero.entry.compareTo(“above”)Returns a positive number.

Page 42: Chapter3 basic java data types and declarations

INDEX

• A POSITION IS USUALLY REFERRED TO AS AN INDEX IN COMPUTER PARLANCE.

Page 43: Chapter3 basic java data types and declarations

ESCAPE CHARACTERS

• SUPPOSE YOU WANT TO OUTPUT A STRING WITH QUOTES INSIDE IT.

• THE WORD “JAVA” NAMES A LANGUAGE, NOT A DRINK!• System.out.println(“The word\”java\” names a language, not just a drink!”);

Page 44: Chapter3 basic java data types and declarations

Escape characters Description

\” DOUBLE QUOTE

\’ SINGLE QUOTE

\\ BACKSLASH

\n NEW LINE GO TO BEGINNING OF THE CURRENT LINE

\r CARRIAGE RETURNGO TO THE BEGINNING OF THE CURRENT LINE

\t TAB WHITESPACE UP TO THE NEXT TAP STOP

Page 45: Chapter3 basic java data types and declarations
Page 46: Chapter3 basic java data types and declarations
Page 47: Chapter3 basic java data types and declarations

println versus print• System.out.println and System.out.print are almost the same

method. The only difference is that with the println method, the next output goes on a new line, whereas with the print method, the next output will be placed on the same line.

• System.out.print(“one ”);System.out.print(“two”);System.out.println(“three ”);output : one two three

Page 48: Chapter3 basic java data types and declarations

Naming Constants• To define a name for a constant, such as a number, place the

reserved words public static final in front of a variable declaration that includes the constant as the initializing value. Place this declaration within the class definition, but outside of the main method and outside of any other method definition.

Syntax:public static final Type Variable = constant;public static final char SCALE = ‘k’;ALTHOUGH IS NOT REQUIRED, IT IS THE NORMAL PRACTICE OF PROGRAMMERS TO SPELL NAMED CONSTANTS USING ALL UPPERCASE LETTERS