1 / 15

Primitive Types

Primitive Types. Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores any value assigned to it. A variable which is declared as a class type

efort
Download Presentation

Primitive Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Primitive Types • Java offers a number of primitive types eg.) int, short, long double, float char • A variable which is declared as a primitive type stores any value assigned to it. • A variable which is declared as a class type can store the reference to an object of that type.

  2. chars • The char type can store one character value 'A', '\n', '\u00E9‘ • char represented by a 16 bit unsigned integer * A total of 216 different Unicode chars can be represented • int value; char letter; value = 5; letter = ‘5’; value = letter; //stores a 53 letter = value; //error ……. Information can be lost // when converting 32 bits to 16

  3. A String is NOT a primitive type.. • String is a class provided by the Java API (A object of this type stores a ordered group of chars) • Strings ARE OBJECTS!! • Look at Java API specs………….

  4. The String class provides constructors String word; word = new String(“hello”); //constructing a //String object from a String literal Seems redundant …. String is the ONLY class that lets you create a String object by assigning a value…. word = “hello”;

  5. String constructors are useful, however String word, word2; word = JOptionPane.showInputDialog(“Enter info:”); The showInputDIalog method calls one of the String constructors to create a String object. This String object is then returned to the caller. word2 = new String(word); You would call a constructor to make a copy of an existing String.

  6. int length() • length() method returns the number of chars in a String object • System.out.println(“size” + “hello”.length()) ; The length of a String is not always obvious… • String in = JOptionPane.showInputDialog(“Enter”); int size = in.length();

  7. String characters are indexed………… The first character in a String is at index 0. Note: last char is at index: length()-1

  8. char charAt(int) • charAt method returns character from a string • the String object is unchanged by this call String in = JOptionPane.showInputDialog(“Enter”); JOptionPane.showMessageDialog(null,”first char is” + in.charAt(0) ); JOptionPane.showMessageDialog(null,”last char is” + in.charAt(in.length()-1 ) );

  9. String substring(int,int) • This method returns a new String object, which contains a portion of the characters of the invoking object • Parameters supply start and “past the end” position String in, apiece; in = JOptionPane.showInputDialog(“Enter”); //user types hello world apiece = in.substring(1,8); // String in is unchanged // String apiece contains characters “ello wo”

  10. A String is an IMMUTABLE object………… Note that we changed the String object that inwas referring to by creating a new object and assigning the new object to in . It is not possible to change the data (chars) stored in a String object. If you wish to do this, you must create a new String object. This is because the String class has no mutator methods (methods which change its instance data). A class without mutator methods is called IMMUTABLE.

  11. int indexOf(char) • indexOf method returns the position of the first occurrence of parameter (-1 if parameter does not occur) String in = JOptionPane.showInputDialog(“Enter”); int blk = in.indexOf(“ “); //where is first blank? If user had entered hello world, blk now contains 5 in = in.substring(blk+1, in.length() ); //in is now referencing an object without the first word

  12. String substring(int) • Many String methods are OVERLOADED … • This call has an implied 2nd parameter – which is the length of the String String in = JOptionPane.showInputDialog(“Enter”); int blk = in.indexOf(“ “); //where is first blank? If user had entered hello world, blk now contains 5 in = in.substring(blk+1 ); //in is now referencing an object without the first word

  13. + is the String concatenation operator • + is an overloaded operator in Java • String fname = "Harry";String lname = "Hacker";String name = fname + lname; • name is "HarryHacker"   • If one operand of + is a string, the other is converted to a string:String a = "Agent";String name = a + 7; • name is "Agent7"  • You commonly use this for output: • System.out.println(“The value is “ + 7 );     

  14. Converting between Strings and Numbers • Convert to number:int n = Integer.parseInt(str);double x = Double.parseDouble(x); • Convert to string: String str = Integer.toString(n); • More efficient, less readable • str = "" + n; • System.out.println(“” + n ); //explicit conversion needed here

  15. Practice • Convert String word to pig latin (assume word stores 1 word) • Ask user for a a sentence If the word COW is contained in the sentence, replace the first occurrence of it with MOOSE • Replace ALL occurrances of COW with MOOSE

More Related