1. int a;
  2. float b;
  3. boolean c;
  4. String d;
  5. a = 5;
  6. b = 3.14;
  7. boolean = true;
  8. d = "Hi";

The above code will define four variables -

  1. A integer
  2. A real number
  3. true or flase
  4. text

Java is a typed language. This means you must say what data type a variable is before you are allowed to use it. This is known as defining the variable. If you do not define a variable you will get a error message. As shown in the example below

  1. int a;
  2. a= 5;
  3. b = a + 1;

Example/src/example/MyFirstProgram.java:24: cannot find symbol
symbol : variable b
location: class example.MyFirstProgram
b = a +1;
1 error
BUILD FAILED (total time: 0 seconds)

Java is saying that you must define a variable before you are allowed to use it. This is a common rookie error. Just remember

If you do not define it you can not use it!

next data types >>

 
Basics
Menu
Search