Thursday, February 12, 2015

Switch

Switch is used if you know the number of options.Please follow the below example


package Example1;

public class SwitchClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String grade="B";
		switch (grade) {
		case "A":
			System.out.println("Excellent");
			break;
		case "b":
		case "B":
			System.out.println("Good");
			break;
		case "C":
			System.out.println("Average");
			break;
		default:
			System.out.println("Better luck next time");
			break;
		}
	}

}


Output:
Good

Please note, It is case sensitive and there is a difference between 'b' and 'B' and you can see that in the example.
Usage of Break ensures it comes out of the loop as soon as the condition is true else it keeps checking all the case block. If no condition matches then it execute the default block. >>>
------------------------------------------------------------------------------------------------------------

Ternary Operator

Ternary operations contains a condition and execute the true or false apart accordingly it also possible to assign it to a variable.


package Example1;

public class TernaryOperator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int x=10;
		int y=20;
		String result=(x
output:
x is less than y
>>>Switch
------------------------------------------------------------------------------------------------------------

Logical Operator

Logical operators are used to perform all the logical operations like and, or not and XOR. Please follow the below example for more details.

package Example1;

public class LogicalOperator {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  if(true && true){
   System.out.println("the value is true");
  }
  if(false || true){
   System.out.println("the value is false");
  }
  if(!(false)){
   System.out.println("the value is true");
  }
  if(true ^ false){
   System.out.println("the value is false");
  }
 }

}


output: the value is true
the value is false
the value is true
the value is false
>>>Ternary
------------------------------------------------------------------------------------------------------------

Relational Operator

This section discuss about Relational operations and you can see the below example with all the kinds of relational operations.


package Example1;

public class RelationalOperator {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int randomnumber=(int)(Math.random()*49);
  
  if(randomnumber<25){
   System.out.println("The number is less than 25");
  }else if(randomnumber > 40){
   System.out.println("The number is greater than 40");
  }else if(randomnumber == 30){
   System.out.println("The number is equal to 30");
  }else if(randomnumber != 10){
   System.out.println("The number is not equal to 10");
  }else if(randomnumber <= 14){
   System.out.println("The number is less than equal to 14");
  }else if(randomnumber >= 14){
   System.out.println("The number is greater than equal to 14");
  }else{
   System.out.println("nothing matched");
  }
  System.out.println("The random number is "+randomnumber);
 }

}


output: Is not predictable since it uses random value
>>>Logical Operator
------------------------------------------------------------------------------------------------------------

Math Function

We have couple of Math functions defined to make math operations more ease. I have listed couple of them in the below example.

package Example1;

public class MathOperator2 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  float x=-10.4f;
  
  //absolute
  System.out.println(Math.abs(x));
  //max and min value
  System.out.println(Math.max(10,100));
  System.out.println(Math.min(10,100));
  //ceil
  System.out.println((int)Math.ceil(100.34));
  //floor
  System.out.println((int)Math.floor(100.34));
  //round
  System.out.println((int)Math.round(100.34));
  //random number
  System.out.println((int)(Math.random()*10));//10 helps you generate number between 1 to 9
 }

}


output:
10.4
100
10
101
100
100
6

>>>
------------------------------------------------------------------------------------------------------------

Math Operators- Increment and Decrement

Increment/Decrement operators are used to increase a decrease the value, well the catch is, is it post or pre increment.
++i is different from i++ and similarly --i is different from i--
lets watch this with an example

package Example1;

public class IncrementOrDecrement {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int x=10;
  System.out.println(x++);first display x value and then increment so now x is 11
  System.out.println(++x);first it increments which would be 12 and then display
 }

}

output:
10
12

package Example1;

public class IncrementOrDecrement {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int x=10;
  System.out.println(x--);first display x value and then decrement so now x is 9
  System.out.println(--x);first it decrements which would be 8 and then display
 }

}

output:
10
8
>>>Math functions
------------------------------------------------------------------------------------------------------------

Math Operators

We have a set of math operators that are used and the below example explains each and every operations.

package Example1;
import java.util.Scanner;
public class MathOperator {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanobj=new Scanner(System.in);
  System.out.println("Enter a number");
  int result=scanobj.nextInt();
  
  //Addition
  int additionvalue=result+result;
  System.out.println(result+"+"+result+"="+additionvalue);
  
  //subtraction
  int subvalue=result-result;
  System.out.println(result+"-"+result+"="+subvalue);
  
  //multiplication
  int multiplicationvalue=result*result;
  System.out.println(result+"*"+result+"="+multiplicationvalue);
  
  //division
  int divisionvalue=result/result;
  System.out.println(result+"/"+result+"="+divisionvalue);
  
  //Modules
  int modulesvalue=result%2;
  System.out.println(result+"%2="+modulesvalue);
 }

}


output:
Enter a number
99
99+99=198
99-99=0
99*99=9801
99/99=1
99%2=1

>>>Math operators
------------------------------------------------------------------------------------------------------------

hasNext

In the previous section we have seen that using scanner object and nextInt() we can retrieve the user value and display. Now how to find what type of value is been entered by the user is it int, float or char? Inorder to find that we use hasNextInt(),hasNextFloat(),etc

Below is the simple example which explains the same.

package Example1;
import java.util.Scanner;

public class hasNextInt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanobj=new Scanner(System.in);
  System.out.println("Enter the value");
  if(scanobj.hasNextInt()){ // check if the entered value is integer 
   int result=scanobj.nextInt();
   System.out.println("You have entered "+result);// if true execute the if block
  }
  else{
   System.out.println("Next time enter a integer");// if false execute the else block
  }
  
 }

}

Output:
Enter the value asdasdasd
Next time enter a integer

If you follow the comments it is self explanatory.Instead of hasNextInt(), you can also use hasNextFloat(),hasNextDouble() etc
>>>Math operators
------------------------------------------------------------------------------------------------------------

System Input Output

Lets Go through System input and output using scanner i.e. take input from the user and display output depending on the input.

- First thing we need to import the scanner package
- Create a scanner object.
- store the value entered by the user in a variable and then you can display or modify the value.
Below is the simple example which explains the same.

package Example1;
import java.util.Scanner;//scanner package

public class hasNextInt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner scanobj=new Scanner(System.in);//create the object
  System.out.println("Enter the value");
   int result=scanobj.nextInt();//store the user entered value in a variable
   System.out.println("You have entered "+result); //display the value
  
}

Output:
Enter the value 10
You have entered 10

If you follow the comments it is self explanatory.Instead of nextInt(), you can also use nextFloat(),nextDouble() etc
>>>hasNext()
------------------------------------------------------------------------------------------------------------

ParseInt

We can use ParseInt,ParseFloat to convert string to any primitive data types.

public class StringtoInt{
public static void main(String[] args) {
  // TODO Auto-generated method stub
  //int to string
  //String to integer
  
  String abc="This is a string";
  System.out.println(abc);
  Byte xyz=10;
  Byte.toString(xyz);
  
  int x=100,y=200;
  String z=Integer.toString(x);//Int to String conversion
  String l=Integer.toString(y);
  System.out.println(z+l);
  int a=Integer.parseInt(z);//String to Int conversion
  int b=Integer.parseInt(l);
  System.out.println(a+b);
 }
}

output is 
100200
300


In the above program we convert int to string with toString method and then again convert string to int with parseInt method. >>>
------------------------------------------------------------------------------------------------------------

to String

It is possible to convert primitive data types to string with the aid of toString. Lets view this with an example.

public class ToStringProgram{

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  int x=100,y=200;
  String z=Integer.toString(x);
  String l=Integer.toString(y);
  System.out.println(z+l);

 }
}


We have two int variables defined which is x and y, next we convert to string and store it in there respective variables z and l. If we perform a concat operation on these variable it is output is a string. Similarly we can perform on float, double,long but not on char and boolean. >>>ParseInt,ParseFloat
------------------------------------------------------------------------------------------------------------

Primitive Data Types

We have 8 primitive data type , lets explore each data types and the maximum and minimum values. -byte -short -int -long -float -double -char -boolean Lets an example with the implementation

public class PrimitiveType  {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //primitive type data

         System.out.println("Maximum byte value "+Byte.MAX_VALUE);//maximum value a byte data type can hold.
  System.out.println("Maximum short value "+Short.MAX_VALUE);
  System.out.println("Maximum integer value "+Integer.MAX_VALUE);
  System.out.println("Maximum float value "+Float.MAX_VALUE);
  System.out.println("Maximum double value "+Double.MAX_VALUE);
  System.out.println("Maximum long value "+Long.MAX_VALUE);
  
  System.out.println("Minimum byte value "+Byte.MIN_VALUE);//minimum value a byte data type can hold.
  System.out.println("Minimum short value "+Short.MIN_VALUE);
  System.out.println("Minimum integer value "+Integer.MIN_VALUE);
  System.out.println("Minimum float value "+Float.MIN_VALUE);
  System.out.println("Minimum double value "+Double.MIN_VALUE);
  System.out.println("Minimum long value "+Long.MIN_VALUE);
  
  char ch=65;
  System.out.println(ch);
  
  boolean boolvalue=true;
  System.out.println(boolvalue);

 }

}

The Above example illustrates the different data types and how to find the min and maximum value of a particular data type. >>>to String
------------------------------------------------------------------------------------------------------------

Final

When a variable is declared as final it remains constant and it cannot be changed. For example consider the name of the company which will never change for any of the employees so this can be defined as final and the value remains same throughout the execution. Let's consider the same example defined in Static section.

AboutStaticVariable.java

public class AboutStaticVariable {

 final static int companyname = "Google";//Defined the class Variable static and final

 public static void main(String args[]) {

  System.out.println(companyname);
  companyname="Apple";//I cannot change this variable value since it is defined as final
 }

}


>>>Primitive Date Types
------------------------------------------------------------------------------------------------------------