Learn Java

 

Object and String type expressions

There are some object type expressions that return an answer, these are called message expressions i.e.

fordFiesta.getSpeed();

The above message send returns the the speed of the object fordFiesta

String objects represent a string of characters and are enclosed in (delimited by) double quotes such as "camera", "hello Jill" or "8493840"

Each String object is made up of individual characters, i.e. camera consists of the following characters 'c' 'a' 'm' 'e' 'r' 'a'

Therefore String literals are delimited by double quotes and char literals are delimited by single quotes

To include a double quote as a character within a String, escape the double quote by preceding it with a back slash, for example "The man said \"hello\" to the shop assistant"

String objects respond to many messages, for example the method toUpperCase() will convert all the characters in a String object to upper case. For example

String tree = "Oak";

"tree".toUpperCase(); will equal "TREE"

tree.toUpperCase(); will equal "OAK"

Example of returning a string representation of non string data types

String.valueOf(2.0); produces a string representation of the double data value 2.0

 

 

 

 

 

 

 

Previous Page Page 4 Next page

 

Page 1 2 3 4 5 6 7