This is the first Java language tutorial in which you will look more closely at a simple program and will basically learn about data types, Before getting started make sure you have any Java compiler installed in your computer. Eclipse is the one in which we will execute our codes. You can simply download it in the link below:
So at this point we are supposing that you have successfully installed a Java complier and are able to run the programs that we will learn during the tutorial.
Firstly you should be conscious that Java is a case sensitive language, which means that capitalizations mistakes (for example writing Public instead of public) will not make the code executable.
Let’s look more closely at the first Java program. The instructions after the two back slash signs ( // )are called comments and they can’t change anything if you run the program. They are mostly used for explanations.
The program :
import java.awt.*; public class firstPrg { public static void main(String[] args) //main accepts an array of strings { System.out.println("Hello, thank you for visiting this blog!"); } } |
This will simply print a message in the console window.
While writing a program you should be careful to import the right package( you will learn more about packages and libraries in other tutorials). Following the keywords public class you write the name of the class (you can not use a keyword as a name of your class, neither use numbers in beginning).
To run the code you will always need the main method in the class you indicate. This method calls all the other methods used to run your program. The code for any method must start with an opening brace { and end with a closing brace }. We will explain the keywords static void in continue, so do not worry about them now. The println() method is essential for printing the messages in the console window.
Data Types
Every variable must be declared to use a data type .There are different data types in Java, basically they are divided in two categories : Primitive Data type and Reference Data Type.
A primitive type is predefined by the language and is named by a reserved keyword.
There are eight primitive data types:
byte: The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive), the default value is 0.
This is an example of declaring a byte data type:
byte num1 = 79;
byte num2 = -22;
short: The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive) , the default value is 0.
Example : short num1 = 999; short num2 = -1100;
int: This data type is large enough for the numbers you will use in you program. The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).
Example : int num1 = 100000; int num2 = -100000;
long: The long data type is even larger then int, it is a 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
Example : long num1= 100000L; int num2 = -100000L;
float: The float data type is a single-precision 32-bit IEEE 754 floating point. It shouldn’t be used for precise values.
Example float num1= 125,7f;
double: The double data type is mostly used for decimal values , it is a double-precision 64-bit IEEE 754 floating point.
Example : double num1 = 2333.56
char: The char data type can’t hold numeric values, it is a single 16-bit, unsigned Unicode character.
Example: char ch = ‘A’;
boolean: The boolean data type represents one bit of information, it has only two possible values: true and false. It is mostly used to track conditions.
Example: boolean answer = true;
Please stay updated by subscribing to our social media pages and RSS while we work on to bring you the next set of tutorials.
By Migena Bufi
About Ali Gajani
Hi. I am Ali Gajani. I started Mr. Geek in early 2012 as a result of my growing enthusiasm and passion for technology. I love sharing my knowledge and helping out the community by creating useful, engaging and compelling content. If you want to write for Mr. Geek, just PM me on my Facebook profile.
Pingback: Goozle Zone()