CHAPTER 3 BASIC JAVA DECLARATIONS
 
 
 
Constant  <ul><li>eggsPerBasket = eggsPerBasket – 2; </li></ul><ul><li>The number 2 in the preceding assignment statement ...
REMINDER <ul><li>numberOfBaskets = dale.readLineInt() </li></ul><ul><li>This is an assignment statement that sets the valu...
REMINDER <ul><li>dale.readLineInt() </li></ul><ul><li>A method is an action, and an invocation of a method causes that act...
 
 
Variable <ul><li>USED TO STORE DATA SUCH AS NUMBERS AND LETTERS. </li></ul><ul><li>THE NUMBER , LETTER OR OTHER DATA ITEM ...
 
 
 
 
 
 
 
 
 
 
 
 
TYPE CASTING <ul><li>IS THE CHANGING OF THE TYPE OF VALUE FROM ITS NORMAL TYPE TO SOME OTHER TYPE SUCH AS CHANGING 2.0 FRO...
REMINDER <ul><li>BE SURE TO NOTE THAT WHEN YOU TYPE CAST FROM double to an int( or from any floating-point type to any int...
 
 
 
String  <ul><li>A String variable is not just a simple variable, like a variable of type int is. A String variable of a cl...
length() <ul><li>Description : returns length of the string object. </li></ul><ul><li>Example :  </li></ul><ul><li>String ...
equals(Other_String) <ul><li>Returns true if the calling object string and the Other_String are equal, considering upperca...
equalsIgnoreCase(Other_String) <ul><li>Returns true if the calling object string and the Other_String are equal, consideri...
ToLowerCase() <ul><li>Returns a string with the same characters as the string object, but with all characters converted to...
toUpperCase() <ul><li>Returns a string with the same characters as the string object, but with all characters converted to...
Trim() <ul><li>Returns a string with the same characters as the string object, but with leading and trailing white space r...
charAt(Position) <ul><li>Returns the character in the string at the Position. Positions are counted 0,1,2 etc. </li></ul><...
Substring(Start) <ul><li>Returns the substring of the string object starting from Start through to the end of the string o...
Substring(Start,End) <ul><li>Returns the substring of the string object starting from position Start through but not inclu...
indexOf(A_String) <ul><li>Returns the position of the first occurrence of the string A_String in the string object. Positi...
indexOf(A_String.Start) <ul><li>Returns the position of the first occurrence of the string in the string object that occur...
LastIndexOf(A_String) <ul><li>Returns the positions of the last occurrence of the string A_String in the string object. Po...
compareTo(A_String) <ul><li>Compares the calling string object and the string object and the string argument to see which ...
INDEX <ul><li>A POSITION IS USUALLY REFERRED TO AS AN  INDEX  IN COMPUTER PARLANCE. </li></ul>
ESCAPE CHARACTERS <ul><li>SUPPOSE YOU WANT TO OUTPUT A STRING WITH QUOTES INSIDE IT. </li></ul><ul><li>THE WORD “JAVA” NAM...
TAB  WHITESPACE UP TO THE NEXT TAP STOP t CARRIAGE RETURN GO TO THE BEGINNING OF THE CURRENT LINE r NEW   LINE   GO TO BEG...
 
 
println  versus  print <ul><li>System.out.println  and  System.out.print  are almost the same method. The only difference ...
Naming Constants <ul><li>To define a name for a constant, such as a number, place the reserved words  </li></ul><ul><li>pu...
Upcoming SlideShare
Loading in...5
×

Chapter3 basic java data types and declarations

2,322

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,322
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter3 basic java data types and declarations

  1. 1. CHAPTER 3 BASIC JAVA DECLARATIONS
  2. 5. Constant <ul><li>eggsPerBasket = eggsPerBasket – 2; </li></ul><ul><li>The number 2 in the preceding assignment statement is called constant. </li></ul>
  3. 6. REMINDER <ul><li>numberOfBaskets = dale.readLineInt() </li></ul><ul><li>This is an assignment statement that sets the value of the variable numberOfBaskets equal to the value returned by the expression </li></ul>
  4. 7. REMINDER <ul><li>dale.readLineInt() </li></ul><ul><li>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. </li></ul>
  5. 10. Variable <ul><li>USED TO STORE DATA SUCH AS NUMBERS AND LETTERS. </li></ul><ul><li>THE NUMBER , LETTER OR OTHER DATA ITEM IN A VARIABLES IS CALLED VALUE. </li></ul>
  6. 23. TYPE CASTING <ul><li>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 . </li></ul><ul><li>Points = (int) distance; </li></ul>
  7. 24. REMINDER <ul><li>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. </li></ul>
  8. 28. String <ul><li>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. </li></ul>
  9. 29. length() <ul><li>Description : returns length of the string object. </li></ul><ul><li>Example : </li></ul><ul><li>String greeting = “Hello!”; </li></ul><ul><li>greeting.length() returns 6. </li></ul>
  10. 30. equals(Other_String) <ul><li>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. </li></ul><ul><li>String greeting = dale.readLine(); </li></ul><ul><li>if (greeting.equals(“Hi”)) </li></ul><ul><li>System.out.println( “Informal Greeting…”); </li></ul>
  11. 31. equalsIgnoreCase(Other_String) <ul><li>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. </li></ul><ul><li>If a program contains, </li></ul><ul><li>String s1 = “mary!”; </li></ul><ul><li>then after this assignment </li></ul><ul><li>s1.equalsIgnoreCase(“Mary!”) </li></ul><ul><li>returns true </li></ul>
  12. 32. ToLowerCase() <ul><li>Returns a string with the same characters as the string object, but with all characters converted to lowercase </li></ul><ul><li>String greeting = “Hi Mary!”; </li></ul><ul><li>greeting.toLowerCase() returns </li></ul><ul><li>“hi mary!” </li></ul>
  13. 33. toUpperCase() <ul><li>Returns a string with the same characters as the string object, but with all characters converted to uppercase. </li></ul><ul><li>String greeting = “Hi Mary!”; </li></ul><ul><li>greeting.toLowerCase() returns </li></ul><ul><li>“HI MARY!” </li></ul>
  14. 34. Trim() <ul><li>Returns a string with the same characters as the string object, but with leading and trailing white space removed. </li></ul><ul><li>String pause = “ hmm ”; </li></ul><ul><li>pause.trim() returns “hmm” </li></ul>
  15. 35. charAt(Position) <ul><li>Returns the character in the string at the Position. Positions are counted 0,1,2 etc. </li></ul><ul><li>String greeting = “Hello!”; </li></ul><ul><li>greeting.charAt(0) returns ‘H’. </li></ul><ul><li>greeting.charAt(1) returns ‘e’. </li></ul>
  16. 36. Substring(Start) <ul><li>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. </li></ul><ul><li>String sample = “abcdefG”; </li></ul><ul><li>Sample.substring(2) returns “cdefG”. </li></ul>
  17. 37. Substring(Start,End) <ul><li>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. </li></ul><ul><li>String sample = “abcdefG”: </li></ul><ul><li>sample.substring(2, 5) returns </li></ul><ul><li>“cdefG”. </li></ul>
  18. 38. indexOf(A_String) <ul><li>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. </li></ul><ul><li>String greeting = “Hi Mary!”; </li></ul><ul><li>greeting.indexOf(“Mary”) </li></ul><ul><li>returns 3. </li></ul><ul><li>greeting.indexOf(“Sally”) </li></ul><ul><li>returns -1. </li></ul>
  19. 39. indexOf(A_String.Start) <ul><li>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. </li></ul><ul><li>String name = “Mary. Mary quite contrary”; </li></ul><ul><li>name.indexOf(“Mary” , 1) </li></ul><ul><li>returns 6. The same value is returned if 1 is replaced by any number up to including 6. </li></ul><ul><li>name.indexOf(“Mary”, 0) </li></ul><ul><li>returns 0. </li></ul><ul><li>Name.indexOf(“Mary” , 8) </li></ul><ul><li>returns -1. </li></ul>
  20. 40. LastIndexOf(A_String) <ul><li>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. </li></ul><ul><li>String name = “Mary , Mary, Mary quite so”; </li></ul><ul><li>name.lastIndexOf(“Mary”) </li></ul><ul><li>returns 12. </li></ul>
  21. 41. compareTo(A_String) <ul><li>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. </li></ul><ul><li>String entry = “adventure”; </li></ul><ul><li>entry.compareTo(“zoo”) returns a negative number </li></ul><ul><li>entry.compareTo(“Adventure”) returns zero. </li></ul><ul><li>entry.compareTo(“above”) </li></ul><ul><li>Returns a positive number. </li></ul>
  22. 42. INDEX <ul><li>A POSITION IS USUALLY REFERRED TO AS AN INDEX IN COMPUTER PARLANCE. </li></ul>
  23. 43. ESCAPE CHARACTERS <ul><li>SUPPOSE YOU WANT TO OUTPUT A STRING WITH QUOTES INSIDE IT. </li></ul><ul><li>THE WORD “JAVA” NAMES A LANGUAGE, NOT A DRINK! </li></ul><ul><li>System.out.println(“The word ”java” names a language, not just a drink!”); </li></ul>
  24. 44. TAB WHITESPACE UP TO THE NEXT TAP STOP t CARRIAGE RETURN GO TO THE BEGINNING OF THE CURRENT LINE r NEW LINE GO TO BEGINNING OF THE CURRENT LINE n BACKSLASH SINGLE QUOTE ’ DOUBLE QUOTE ” Description Escape characters
  25. 47. println versus print <ul><li>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. </li></ul><ul><li>System.out.print(“one ”); </li></ul><ul><li>System.out.print(“two”); </li></ul><ul><li>System.out.println(“three ”); </li></ul><ul><li>output : </li></ul><ul><li>one two </li></ul><ul><li>three </li></ul>
  26. 48. Naming Constants <ul><li>To define a name for a constant, such as a number, place the reserved words </li></ul><ul><li>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. </li></ul><ul><li>Syntax: </li></ul><ul><li>public static final Type Variable = constant; </li></ul><ul><li>public static final char SCALE = ‘k’; </li></ul><ul><li>ALTHOUGH IS NOT REQUIRED, IT IS THE NORMAL PRACTICE OF PROGRAMMERS TO SPELL NAMED CONSTANTS USING ALL UPPERCASE LETTERS </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×