Download Java Programming: Strings, Command Line Arguments, and Data Types and more Lecture notes Web Design and Development in PDF only on Docsity! Handout 3-1 Web Design & Development CS-506 - 27 - Learning Basics Strings A string is commonly considered to be a sequence of characters stored in memory and accessible as a unit. Strings in java are represented as objects. String Concatenation “+” operator is used to concatenate strings – System.out.pritln(“Hello” + “World”) will print Hello World on console String concatenated with any other data type such as int will also convert that datatype to String and the result will be a concatenated String displayed on console. For example, – int i = 4; – int j = 5; System .out.println (“Hello” + i) will print Hello 4 on screen – However System,.out..println( i+j) ; will print 9 on the console because both i and j are of type int. Comparing Strings For comparing Strings never use == operator, use equals method of String class. – == operator compares addresses (shallow comparison) while equals compares values (deep comparison) E.g string1.equals(string2) Example Code: String concatenation and comparison public class StringTest { public static void main(String[] args) { int i = 4; int j = 5; System.out.println("Hello" + i); // will print Hello4 System.out.println(i + j); // will print 9 String s1 = new String (“pakistan”); String s2 = “pakistan”; if (s1 == s2) { System.out.println(“comparing string using == operator”); } Handout 3-1 Web Design & Development CS-506 - 28 - if (s1.equals( s2) ) { System.out.println(“comparing string using equal method”); } } } On execution of the above program, following output will produce Handout 3-3 Web Design & Development CS-506 - 31 - Primitives vs Objects • Everything in Java is an “Object”, as every class by default inherits from class “Object” , except a few primitive data types, which are there for efficiency reasons. • Primitive Data Types o Primitive Data types of java boolean, byte 1 byte char, short 2 bytes int, float 4 bytes long, double 8 bytes • Primitive data types are generally used for local variables, parameters and instance variables (properties of an object) • Primitive datatypes are located on the stack and we can only access their value, while objects are located on heap and we have a reference to these objects • Also primitive data types are always passed by value while objects are always passed by reference in java. There is no C++ like methods – void someMethod(int &a, int & b ) // not available in java Stack vs. Heap Stack and heap are two important memory areas. Primitives are created on the stack while objects are created on heap. This will be further clarified by looking at the following diagram that is taken from Java Lab Course. num st 5 0F59 0F59 name ali Stack Heap int num = 5; Student s = new Student(); Handout 3-3 Web Design & Development CS-506 - 32 - Wrapper Classes Each primitive data type has a corresponding object (wrapper class). These wrapper classes provides additional functionality (conversion, size checking etc.), which a primitve data type cannot provide. Wrapper Use You can create an object of Wrapper class using a String or a primitive data type • Integer num = new Integer(4); or • Integer num = new Integer(“4”); Note: num is an object over here not a primitive data type You can get a primitive data type from a Wrapper using the corresponding value function • int primNum = num.intValue(); Converting Strings to Numeric Primitive Data Types To convert a string containing digits to a primitive data type, wrapper classes can help. parseXxx method can be used to convert a String to the corresponding primitive data type. String value = “532”; int d = Integer.parseInt(value); String value = “3.14e6”; double d = Double.parseDouble(value); Primitive Corresponding Data Type Object Class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean Handout 3-3 Web Design & Development CS-506 - 33 - The following table summarizes the parser methods available to a java programmer. Data Type Convert String using either … byte Byte.parseByte(string) new Byte(string).byteValue() short Short.parseShort(string) new Short(string).shortValue() int Integer.parseInteger(string) new Integer(string).intValue() long Long.parseLong(string) new Long(string).longValue() float Float.parseFloat(string) new Float(string).floatValue() double Double.parseDouble(string) new Double(string).doubleValue()