Java Operators and Java SE 7 Binary Literals

Java SE 7 Binary Literals







Binary literals are Java int values.  A cast is required when the integer value of the literal exceeds the greatest non-negative value that the type can be hold. For Example 

byte aByte = 0b0111_1111; // aByte is 127

byte aByte  =  0b1000_0000; // compiler error - a cast is required //(  value is -128)



OPERATORS

  • Simple assignment operator
     = Simple assignment operator
  • Arithmetic operators
     + Additive operator (also used for String concatenation)
      - Subtraction operator
      * Multiplication operator
      / Division operator
      % Remainder operator
  • Unary operators
      + Unary plus operator; indicates positive
       - Unary minus operator; indicates negates an expression
      ++ Increment operator; increments a value by 1
       -- Decrement operator; decrements a value by 1
       ! Logical complement  operator; inverts the value of a Boolean

Because numbers have been introduced, the slide shows a  list of common operators. Most are common to any programming language, and a description of each is provided in the slide.


Strings
public class Strings{

public static void main(String args[]){

char letter="a";

String string1 ="Hello";
String string2 =" World";
String string3 =" ";
String dontDoThis = new String (" Bad Practice");

string3 = string1 + string2; //Concatenate strings

System.out.println("Output: "+ string3 + " "+ letter);

}
}

The code in the slide demonstrates how text characters are represented in Java.
Single characters can be represented with the  char type.
However, Java also includes a String  type for representing multiple characters .
String can be defined as shown in the slide and combined using the ''+'' sign as a concatenation operator.

The output from the code in the slide is :
HelloWorld a 



String Operations

public class StringOperations{

public static void main(String [] args){

String string2 = "World";
String string3 =  " ";

string3 = "Hello".concate(string2);
System.out.println("string3: " + string3);

// Get Length
System.out.println("Length: "+ string1.length());

// Get SubString
System.out.println("Sub: "+ string3.substring(0,5));

//Uppercase
System.out.println("Upper "+string3.toUpperCase());

}
}

This slide demonstrate some common string methods 
  • concat()
  • length()
  • substring()
  • toUpperCase()

The output from the program is :
string3 : HelloWorld

Length : 5

Sub : Hello

Upper : HELLOWORLD

Note: String is a class , not  a primitive type. Instances of a class  String represent sequences of Unicode characters. String literals are stored as String objects  and "interned" meaning that for strings with matching characters , they all point to the  same String object.


if else

public class IfElse{

public static void main( String args[]){


long a =1;
long b=2;

if( a==b){

System.out.println("True");

}else{

System.out,println("False"):

}
}
}



The output from the code in the slide is:

False

About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment