Comments in a Java Program Java Overview Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over multiple lines like C Example: /* This is a multiple line comment. */ Comments can be special Java comments that help produce java documentation using the javadoc utitlity program. Example: /** comments f an HTML page goes here */ Identifiers Can be a series of characters consisting of letters, digits, undersces(_) and dollar signs ($). It does not begin with a digit and does not contain spaces and cannot be a reserved wd. Examples: WelcomeApplet, $value, _value, my_inputfield, button8 Invalid Examples: 8button input field Identifier Conventions Class names begin with a capital letter and have a capital letter f every wd in the class name ( Ex: WelcomeAppletClass) Variables and Methods begin with lower case letter and have a capital letter f every wd in the variable ( Ex: firstnumber, and myfirstmethod) Constants are all caps with an undersce separating individual wds ( Ex: RATE_PER_HOUR) Primitive Data Types and Declaring Variables Integer values (32 bits from -2 31 to 2 31-1): int number1, number2; Real values (32 bits): float area; True/False values (8 bits): boolean done=true; Primitive Data Types Continued Other Integers: byte (8 bits), sht (16 bits) and long (64 bits) byte verysmallinteger; sht numberinclass; long verybiginteger; Other real values: double (64 bits) double nationaldebt; By default, real values are double. F example, 5.0 is considered to be double not float. To make it float, write it as 5.0f 1
int i = 34; long l = 1000000; float f = 100.2f; float f = 100.2F; double d = 100.2; double d = 100.2D; Double d=100.2d; Number Literals Primitive Data Types Continued Character data uses ISO Unicode Character set (16 bits) Established by the Unicode Constium to suppt the interchange, processing, and display of texts f the wld s diverse languages. (see www.unicode.g) Ranges from \u0000 to \uffff ASCII is a subset of Unicode ( \u0000 to \u007f cresponds to the 128 ASCII characters Examples: char letter='a'; //A char letter='\u0041'; //A char letterpi='\03a0'; // Constants static final datatype CONSTANT_NAME = valueofconstant; static final double PI = 3.14159; static final int SIZE = 3; Shtcut Operats Operat Example Equivalent += i+=8 i = i+8 -= f-=8.0 f = f-8.0 *= i*=8 i = i*8 /= i/=8 i = i/8 %= i%=8 i = i%8 Increment and Decrement Operats x = 1; y = 1 + x++; y = 1 + ++x; y = 1 + x--; y = 1 + --x; Casting Operat Precedence ++, -- *, /, % +, - <, <=, >, => ==,!=; && =, +=, -=, *=, /=, %= 2
Consider the following statements: byte i = 100; long l = i*3+4; double f = i*3.1+l/2; When perfming a binary operation involving operands of different types, Java automatically converts the operand of a smaller range type to a larger range type of the other operand. Example: If one is int and the other is float, the int is converted to float Casting is an operation that converts a value of one data type into a value of another data type. Casting a variable of a type with a small range to a variable with a larger range is known as widening a type. Casting a variable of a type with a large range to a variable with a smaller range is known as narrowing a type. Widening a type can be perfmed automatically. Narrowing a type must be perfmed explicitly. The String Class Examples: float f = 10.1; //illegal float f = 10.1f; //legal float f = (float)10.1; //explicit casting java.lang.string is a class that models a sequence of characters as a string. The String class contains 11 constructs and > 40 methods Examples: String message = Welcome to Java! ; String class String sequence of character data String objects are immutable they cannot be changed once they have been created String class has 11 constructs String () String (String s) Strings Instantiating a string object String name = Joe ; String name = new String ( Joe ); 3
char charat (int position) Return the character located at the specified position char c = name.charat(2); equals if (s1.equals(s2)) int length() Returns the number of characters in the string int l = name.length(); String substring (int start) String substring (int start, int end) Used to extract a substring from a larger string start is the position of the first character to extract end is NOT the position of the last character to extract but the position following the last character to extract Assume: String letters = abcdef ; System.out.println (letters.substring (3)); Prints def System.out.println(letters.substring (0,3)); Prints abc System.out.println (letters.substring (2,4)); Prints cd boolean equals (Object o) Return true if the object is a string that has the same length and contains the same characters as the string f which the method is called boolean equalsignecase (Object o) int compareto(string s) Returns 0 if both strings are the same Returns a positive number if the argument string comes first Returns a negative number if the calling string come first indexof and lastindexof searches f a specified character substring in a String Example: String letters = abcdefabcd ; int i=letters.indexof( c ); // i contains 2 int i=letters.indexof( $ ); // i contains -1 int i=letters.indexof ( a,1); //starts with 1 and searches f a i contains 6 int i=lastindexof ( b ); //i=7 int i=letters.indexof( def ); // i=3 4
replace (char,char), touppercase(), trim() String s1 = hello ; s1 = s1.replace ( l, L ); //returns a string object in which every occurrence of l is replaced by L s1 = s1.touppercase(); // HELLO s1 = s1.trim() //remove any white spaces at beginning end 5