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.
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
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………….
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”;
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.
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();
String characters are indexed…………
The first character in a String is at index 0.
Note: last char is at index: length()-1
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 ) );
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”
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 .
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.
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
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
+ 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 );
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
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