Transcript
Page 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

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.

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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();

Page 7: 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

String characters are indexed…………

The first character in a String is at index 0.

Note: last char is at index: length()-1

Page 8: 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

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

Page 9: 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

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”

Page 10: 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

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.

Page 11: 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

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

Page 12: 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

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

Page 13: 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

+ 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 );     

Page 14: 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

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

Page 15: 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

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


Top Related