Monday, May 27, 2013

How to check datatype in java!

Hi community!
I have sample code for checking datatype in java. Let's go to check it!

public class DataTypeInJava {
public static void main(String[] args) {

System.out.println("\nFor byte datatype");
System.out.println("Max "+ Byte.MAX_VALUE);
System.out.println("Mix "+ Byte.MIN_VALUE);
System.out.println("Size :"+ Byte.SIZE+ "bits");

System.out.println("\nFor short datatype");
System.out.println("Max "+ Short.MAX_VALUE);
System.out.println("Mix "+ Short.MIN_VALUE);
System.out.println("Size :"+ Short.SIZE+ "bits");

System.out.println("\nFor int datatype");
System.out.println("Max "+ Integer.MAX_VALUE);
System.out.println("Mix "+ Integer.MIN_VALUE);
System.out.println("Size :"+ Integer.SIZE+ "bits");

System.out.println("\nFor long datatype");
System.out.println("Max "+ Long.MAX_VALUE);
System.out.println("Mix "+ Long.MIN_VALUE);
System.out.println("Size :"+ Long.SIZE+ "bits");

System.out.println("\nFor float datatype");
System.out.println("Max "+ Float.MAX_VALUE);
System.out.println("Mix "+ Float.MIN_VALUE);
System.out.println("Size :"+ Float.SIZE+ "bits");

System.out.println("\nFor double datatype");
System.out.println("Max "+ Double.MAX_VALUE);
System.out.println("Mix "+ Double.MIN_VALUE);
System.out.println("Size :"+ Double.SIZE+ "bits");

System.out.println("\nFor boolean datatype");
System.out.println("Type "+ Boolean.FALSE +" and "+ Boolean.TRUE);

System.out.println("\nFor character datatype");
System.out.println("Size "+ Character.SIZE);
System.out.println("Type "+ Character.TYPE);


}
}

Sample Game in Java!

Sample Game:

First way!

import java.util.Random;
public class Player {
String result="";String name="";
public String player(){
Random rnd = new Random();
int i = rnd.nextInt(3);
if(i == 0)
result = "Paper";
else if(i == 1)
result = "Crisor";
else
result = "Rock";
return result;
}
public static void main(String[] args) {
Player p1 = new Player();
Player p2 = new Player();

p1.name = "Player1";
p2.name = "Player2";

String pla1 =p1.player();; String pla2=p2.player();
if(pla1 == "Crisor" && pla2 == "Paper"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p1.name + " is win!");
}else if(pla1 == "Rock" && pla2 == "Crisor"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p1.name + " is win!");
}else if(pla1 == "Paper" && pla2 == "Rock"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p1.name + " is win!");
}else if(pla2 == "Crisor" && pla1 == "Paper"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p2.name + " is win!");
}else if(pla2 == "Rock" && pla1 == "Crisor"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p2.name + " is win!");
}else if(pla2 == "Paper" && pla1 == "Rock"){
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2);
System.out.println(p2.name + " is win!");
}else{
System.out.println(p1.name + " is " + pla1 + " and " + p2.name + " is " + pla2+".Plase play again!");
}
}
}