// Define our new java class
// Since we call it Class1 we must name our java file Class1.java
public class Class1
{
// The main function (called when you execute your program)
public static void main(String[] args)
{
// declare a new variable myNumber as integer
// it can now hold numbers (..,-2,-1,0,1,2,3,...n).
// the default value = 0.
int myNumber;
// declare a new variable myDecimalNumber as double
// it can now hold decimalnumbers ( -2.7 or 1.5)
double myDecimalNumber;
// declare a new variable myChar as char. It can hold a character.
char myChar;
// declare a new variable myBool as boolean
// it can hold true or false (1 or 0). The default value = false.
boolean myBool;
// define the variable myNumber and set value equal to 5
myNumber = 5;
// define the variable myDecimalNumber and set value equal to 8.8;
myDecimalNumber = 8.8;
// define the variable myChar as the character a.
myChar = 'a';
// define the variable myBool as true.
myBool = true;
// Display myNumber
System.out.println("my number: " + myNumber);
// Display myDecimalNumber
System.out.println("my decimal number: " + myDecimalNumber);
// Display myChar
System.out.println("my character: " + myChar);
// Display myBool
System.out.println("my boolean: " + myBool);
}
}
// Learn to declare and define different variables and to display them.
Download Java Source Code
I hope you found this java tutorial useful!
Don't forget to mention Apron Tutorials in your References!