chapter3 basic java data types and declarations

Post on 19-May-2015

2.202 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CHAPTER 3 BASIC JAVA DECLARATIONS

Constant

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

statement is called constant.

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

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.

Variable

• USED TO STORE DATA SUCH AS NUMBERS AND LETTERS.

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

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;

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.

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.

length()

• Description : returns length of the string object.

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

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…”);

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

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!”

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!”

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”

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

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

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

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.

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.

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.

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.

INDEX

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

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!”);

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

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

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

top related