ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Creating a Variable in Java

Updated on January 14, 2013

Java is a general purpose program which is 100% object oriented. To test run a Java program in your system you have to install Java Running Environment or a Java Software Development Kit in your system other wise you can run in online Interpreters or Compilers.

How to create a variable in Java ?

The general syntax for creating a variable in Java is

data_type variable_name = value

were data type can be

  • boolean - which takes the value true/false in 1 bit memory, its default value is false
  • char - which stores Unicode character its size is 16-bit and default value is \u0000
  • byte - to store the Signed Integer in the 8-bit memory space
  • short - for storing Signed Integer in 16-bit memory space
  • int - to store Signed Integer in 32-bit , ie numbers between +/-215 -1 (ie +/-32,768)
  • long - stores Signed integer in 64-bit memory space
  • float - for storing Real Numbers in 32-bit ( between +/- 3.4 * 1038)
  • double - for storing Real Numbers in 64-bit (between +/- 1.8 * 10308)
  • String - for storing a Sting of character (Note: S should be capital)

An example of each

boolean bool=true;

char letter=A;

byte count=0;

short number=12;

int days=12;

long salary =40000;

float percentage =98.01;

double distance=41667.58;

A Simple Program using java

public class sample{
public static void main(String[] args){
int i=5;
String mystring="Favorite number";
System.out.print("My "+mystring+" is "+i);

Output : My Favorite number is 5

Did you use online Compilers before ?

See results


    0 of 8192 characters used
    Post Comment

    No comments yet.