Friday, May 29, 2009

Java Basics Using Qt Jambi Tutorial

Numbers

Number class is used so you can utilize its methods for conversion or when an argument of a method expects an object.


Example of converting numeric values to numeric types using number class:

Number num=10; //create number data type

int numInt = num.intValue(); //convert Number to int data type

System.out.print(numInt);


Example of converting string to integer using integer class:

String s=”20”; //declare string variables to “20”

int numInt=Integer.parseInt(s); //converts string s to integer numInt

System.out.println(numInt); //print the int


Strings

From the previous example, we declared a string variable String s=”20”; The “20” Here is a string since it is enclosed by “”, which signifies it is a string not an integer or number and it is accompanied by the keyword String. We can also say String s=”hello there!”


Getting the Length of a String

Ex.

String s=”hello!”; //declare s as String and assign it to “hello!”

System.out.println(s.length()); //s.length since we assigned “hello!” to s, we access the String class method length to get the length of the string. System.out.println() prints the string.


Concatenating Strings or Joining Strings

Ex.

String s=”helloˍ̱̱̱“; //(“ˍ̱̱̱” is a space) declare s as string and assign s to “helloˍ̱̱̱

System.out.println(s.concat(“world!”)); //concatenates or joins the string from s (which is hello) to “world!”, then prints the joined string.


Another way is,

String s=”helloˍ̱̱̱“ + “world!” //by using + to join or concatenate strings.


String Formatting

Example:

double doubleVar = 10.5; //declare and assign double 10.5.

int intVar=20; //declare, assign intVar to 20.

String stringVar=”hello!”; //declare and assign string var to “hello”

String fs; //declare fs as String.

fs=String.format (“The value of double var is %f, while the value of the “ + “integer variable is %d, and string is “+ “%s', doubleVar,intVar,stringVar); //Use method format of String class %f to assign the variable doubleVar, %d to intVar, %s to stringVar. These %f,%d,%s are called format specifiers which replaces the value in sequence after the (“”,)


Ex.

int i =10; //declare and assign value 10 to I;

String s = String.valueOf(i); //converts I to String using valueOf().

Note: you can use valueOf when converting to byte,integer,double,float,long and short.


Ex.

String s =”20”; //declare and assign string “20” to s.

integer a = Integer.valueOf(s); //converts s(20) to Integer a.


Getting Parts of a String


Ex.

String s = “hello world!”; //assign “hello world” to s as string.

String ss = s.substring(5,10); //returns “world”


0 1 2 3 4 5 6 7 8 9 10

h e l l o w o r l d

Note: Just remember the 5 is inclusion, and 10 is exclusion.


Other Methods You Can Use:


indexOf() //returns the index number of the string or char specified.

charAt() //returns the character where the index is specified.

trim() //removes the leading and trailing spaces.

toLowerCase() //returns a copy of string to lowercase.

toUpperCase() //returns a copy of string to uppercase.

to be continued...