Classes Basics
Contents
Introduction
This chapter examines the operators used in the Java language. We have already used some operators such as "=" and "+" in previous sections.Arithmetic
The "%" modulus operatos takes 2 argument and returns the remainder.Example: 10 % 3 produces 1 10 % 5 produces 0We also have the addition assignment operator "+=" . This adds the number on the right to the value of the variable on the left and assigns it the sum valu to the value on the left.
int x1 = 5 ; x1 += 2 ; //x1 is now 7 //Same as x1 = x1 + 2 ; x1 -= 3 ; //Same as x1 = x1 - 3 ; Also x1 *= 2 ; //Same as x1 = x1 * 2 ; x1 /= 2 ; //Same as x1 = x1 / 2 ;
Auto increment
This operator can increment or decrement a value. We have 2 types "postfix" and "prefix". The difference is the point of time that the variable is updated at.int x1 = 10 ; //Prints 11 System.out.println( ++x1 ) ; //Prints 11 System.out.println( x1 ) ; //Prints 11 //Change is not done at the below line. System.out.println( x1++ ) ; //Change is made at this point //Prints 12 System.out.println( x1 ) ;
File: Increment.java
public class Increment { public static void main( String args[] ) { int x1 = 5 ; int y1 = 5 ; int z1 ; //x1 is still 5 and y1 becomes 6 so z1 is 11 z1 = x1++ + ++y1 ; //z1 11 //Now x1 is 6 y1 is 6 //z1 prints 11 System.out.println( "z1 = " + z1 ) ; //x1 is 6 and y1 is 6 z1 = x1++ + y1++ ; //z1 is 12 // x1 is now 7 and y1 is 7 System.out.println( "z1 = " + z1 ) ; z1 = x1 + y1 ; //z1 is now 14 System.out.println( "z1 = " + z1 ); //z1 is still 14 and will only change it's value // after the conditional expression if ( z1++ == 14 ) z1++ ; //After the conditional expression z1 is 15 and now we have another "++" so z1 is 16 ++z1 ; //Now z1 is 17 System.out.println( "z1 = " + z1 ) ; //17 } } //class C:\WebSite\Learn\2\Java\intro>java Increment z1 = 11 z1 = 12 z1 = 14 z1 = 17Similar to the increment operators we have the decrement operators.
File: Decrement.java
public class Decrement { public static void main( String args[] ) { int x1 = 12 ; int y1 = 12 ; int z1 ; z1 = x1-- + --y1 ; System.out.println( "z1 = " + z1 ); z1 = x1-- + y1-- ; System.out.println( "z1 = " + z1 ); z1 = x1 + y1 ; System.out.println( "z1 = " + z1 ); if ( z1-- == 20 ) z1-- ; // After the conditional expression z1 is 19 // and now we have another "--" so z1 is 18 --z1 ; //Now z1 is 17 System.out.println( "z1 = " + z1 ); } } //class C:\WebSite\Learn\2\Java\intro>java Decrement z1 = 23 z1 = 22 z1 = 20 z1 = 17
Comparision
File: Relational.java
public class Relational { public static void main( String args[] ) { int x1 = 5 ; int y1 = 5 ; Integer n1 = new Integer(47); Integer n2 = new Integer(47); //We know the below is false because //== compares addresses and not the contents System.out.println(n1 == n2); System.out.println(n1 != n2); //Must use equals System.out.println( n1.equals(n2) ); System.out.println( "x1 < y1 :" + (x1 < y1) ); System.out.println( "x1 <= y1 :" + (x1 <= y1) ); //True if x1 is not equal to y1 System.out.println( "x1 != y1 :" + (x1 != y1) ); y1 = 6 ; //&& Check if both conditions are true System.out.println( "((x1 < y1) && (y1 == 6 )) :" + ((x1 < y1) && (y1 == 6 )) ); //|| Check if one condition is true System.out.println( "((x1 < y1) || (y1 == 6 )) :" + ((x1 < y1) || (y1 == 6 )) ); //Short circuiting //If the first condition is false then the result will be //false so no need to evaluate (y1 == 6) System.out.println( "((x1 > y1) && (y1 == 6) ) :" + ((x1 > y1) && (y1 == 6 )) ); //If the first condition is true the second condition //is not evaluated. System.out.println( "((x1 < y1) || (y1 == 6 )) :" + ((x1 < y1) || (y1 == 6 )) ); } } //class C:\WebSite\Learn\2\Java\intro>java Relational false true true x1 < y1 :false x1 <= y1 :true x1 != y1 :false ((x1 < y1) && (y1 == 6 )) :true ((x1 < y1) || (y1 == 6 )) :true ((x1 > y1) && (y1 == 6) ) :false ((x1 < y1) || (y1 == 6 )) :true
Bitwise Operators
File: Binary.java
public class Binary { public static void main( String args[] ) { int i1 = 0x2f; //2f is a hexadecimal System.out.println( "i1: " + i1 ) ; // 6.022 * 10 power 23 double largeValue = 6.022e23; System.out.println( "largeValue: " + largeValue ) ; int binaryNumber1 = 0b1101; int binaryNumber2 = 0b0100; //Operator & 2 bits and to 1 if both are 1 else //the result is 0 System.out.println( "binaryNumber1 & binaryNumber2 " + (binaryNumber1 & binaryNumber2) ) ; System.out.println( Integer.toBinaryString( binaryNumber1 & binaryNumber2) ) ; //Operator | on 2 bits and is 1 if any bit is 1 System.out.println( "binaryNumber1 | binaryNumber2: " + (binaryNumber1 | binaryNumber2) ) ; System.out.println( Integer.toBinaryString( binaryNumber1 | binaryNumber2) ) ; byte binaryNumber3 = (byte)0b00000101; byte binaryNumber4 = (byte)0b11111111; System.out.println( binaryNumber3 ) ; System.out.println( binaryNumber4 ) ; binaryNumber3 = (byte)( binaryNumber3 << 1 ) ; System.out.println( binaryNumber3 ) ; //Since the no is negative 1's are inserted to the left binaryNumber4 = (byte)( binaryNumber4 >> 1 ) ; System.out.println( binaryNumber4 ) ; // binaryNumber4 is still -1 at this point // 0 is inserted on the left hand side System.out.println( "---" ) ; System.out.println( binaryNumber4 ) ; // binaryNumber4 is -1 so expecting 0's inserted on the //left hand side //Works properly System.out.println( binaryNumber4 >>> 1 ) ; binaryNumber4 = (byte)( binaryNumber4 >>> 1 ) ; System.out.println( binaryNumber4 ) ; System.out.println( binaryNumber4 >>> 1 ) ; //Only retains the right most 8 bits which //are all ones System.out.println( (byte)binaryNumber4 ) ; System.out.println( "---" ) ; System.out.println( binaryNumber4 >>> 1 ) ; binaryNumber4 = (byte)( (byte)binaryNumber4 >>> 1 ) ; } } //class C:\WebSite\Learn\2\Java\intro>java Binary i1: 47 largeValue: 6.022E23 binaryNumber1 & binaryNumber2 4 100 binaryNumber1 | binaryNumber2: 13 1101 5 -1 10 -1 --- -1 2147483647 -1 2147483647 -1 --- 2147483647
String
File: StringOp.java
public class StringOp { public static void main( String args[] ) { int x1 = 1 , x2 = 2 , x3 = 3 ; String str1 = "Int Variables" ; String fName = "Archie " ; String lName = "Moore" ; //+ is the string concatenation operator //println converts the variables to integers System.out.println( str1 + x1 + x2 + x3 ) ; System.out.println( str1 + (x1 + x2 + x3) ) ; String str2 ; //StringOp.java:17: error: incompatible types: int cannot be converted to String // No automatic conversion // str2 = x1 ; str2 = Integer.toString( x1 ) ; //concatenate 2 strings using + String fullName = fName + lName ; System.out.println( fullName ) ; //Can use the += operator fullName += " Boxer" ; System.out.println( fullName ) ; } } //class
Rounding
public class Rounding { public static void main( String args[] ) { int x1 = 1 , x2 = 2 , x3 = 3 ; float f1 = 0.7f ; float f2 = 0.3f ; //By default .7 is a double not a float double d1 = 0.7 ; //int casts truncates the number System.out.println( (int)f1 ) ; System.out.println( (int)f2 ) ; System.out.println( (int)d1 ) ; System.out.println( Math.round(f1) ) ; System.out.println( Math.round(f2) ) ; System.out.println( Math.round(d1) ) ; } } //class