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

Download 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

Post on 13-Jan-2016

213 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Primitive TypesJava offers a number of primitive types eg.) int, short, long double, float charA variable which is declared as a primitive type stores any value assigned to it.</p><p>A variable which is declared as a class type can store the reference to an object of that type.</p></li><li><p>charsThe char type can store one character value 'A', '\n', '\u00E9</p><p>char represented by a 16 bit unsigned integer</p><p>* 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 </p></li><li><p>A String is NOT a primitive type..</p><p>String is a class provided by the Java API (A object of this type stores a ordered group of chars)</p><p>Strings ARE OBJECTS!!</p><p>Look at Java API specs.</p></li><li><p>The String class provides constructors String word; word = new String(hello); //constructing a //String object from a String literal</p><p> Seems redundant .</p><p> String is the ONLY class that lets you create a String object by assigning a value.</p><p> word = hello;</p></li><li><p>String constructors are useful, however String word, word2; word = JOptionPane.showInputDialog(Enter info:);</p><p> The showInputDIalog method calls one of the String constructors to create a String object. This String object is then returned to the caller.</p><p> word2 = new String(word);</p><p> You would call a constructor to make a copy of an existing String.</p></li><li><p>int length()length() method returns the number of chars in a String object</p><p>System.out.println(size + hello.length()) ;</p><p> The length of a String is not always obvious</p><p>String in = JOptionPane.showInputDialog(Enter); int size = in.length();</p></li><li><p>String characters are indexed The first character in a String is at index 0.</p><p>Note: last char is at index: length()-1</p></li><li><p>char charAt(int)charAt method returns character from a string the String object is unchanged by this call</p><p> String in = JOptionPane.showInputDialog(Enter); </p><p> JOptionPane.showMessageDialog(null,first char is + in.charAt(0) );</p><p> JOptionPane.showMessageDialog(null,last char is + in.charAt(in.length()-1 ) );</p></li><li><p>String substring(int,int)This method returns a new String object, which contains a portion of the characters of the invoking object</p><p>Parameters supply start and past the end position </p><p>String in, apiece;in = JOptionPane.showInputDialog(Enter); //user types hello world</p><p>apiece = in.substring(1,8);</p><p>// String in is unchanged// String apiece contains characters ello wo</p></li><li><p>A String is an IMMUTABLE object Note that we changed the String object that in was referring to by creating a new object and assigning the new object to in .</p><p> 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.</p><p>This is because the String class has no mutator methods (methods which change its instance data). A class without mutator methods is called IMMUTABLE. </p></li><li><p>int indexOf(char)indexOf method returns the position of the first occurrence of parameter (-1 if parameter does not occur) </p><p>String in = JOptionPane.showInputDialog(Enter);</p><p>int blk = in.indexOf( ); //where is first blank? If user had entered hello world, blk now contains 5</p><p>in = in.substring(blk+1, in.length() ); //in is now referencing an object without the first word</p></li><li><p>String substring(int)Many String methods are OVERLOADED This call has an implied 2nd parameter which is the length of the String</p><p>String in = JOptionPane.showInputDialog(Enter);</p><p>int blk = in.indexOf( ); //where is first blank? If user had entered hello world, blk now contains 5</p><p>in = in.substring(blk+1 ); //in is now referencing an object without the first word</p></li><li><p>+ is the String concatenation operator+ is an overloaded operator in Java</p><p>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"</p><p>You commonly use this for output:System.out.println(The value is + 7 ); </p></li><li><p>Converting between Strings and NumbersConvert to number: int n = Integer.parseInt(str); double x = Double.parseDouble(x); </p><p>Convert to string: String str = Integer.toString(n); </p><p>More efficient, less readable str = "" + n; </p><p>System.out.println( + n ); //explicit conversion needed here</p></li><li><p>Practice</p><p>Convert String word to pig latin (assume word stores 1 word)</p><p>Ask user for a a sentence If the word COW is contained in the sentence, replace the first occurrence of it with MOOSE</p><p>Replace ALL occurrances of COW with MOOSE </p></li></ul>