Java for Beginners
Programmers usually work in so-called Integrated Development Environment (IDE). You can write, compile and run programs there. IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse. You can download it from the Web site www.eclipse.org. After the downloading, try to create a Hello World application using the Eclipse instructions.
Clarification based on the Hello World application, created in Eclipse:
Each method starts with a declaration line called a method signature:
public static void main(String[] args)
This method signature tells us the following:
- Who can access the method - public. The keyword public means that the method main() could be accessed by any other Java class or JVM itself.
- Instructions on how to use it - static. The keyword static means that you don’t have to create an instance (a copy ) of HelloWorld object in memory to use this method.
- Does the method return any data? The keyword void means that the method main() doesn’t return any data to the calling program, which is Eclipse in this case. But if for example, a method had to perform some calculations, it could have returned a resulting number to its caller.
- The name of the method is main.
- The list of arguments – some data that could be given to the method - String[] args. In the method main() the String[] args means that this method can receive an array of Strings that represent text data. The values that are being passed to a method are called arguments.
methods startGame(), stopGame(), readScore(), and so on.
The body of our method main() has only one line :
System.out.println("Hello World");
Every command or a method call must end with a semicolon ;. The method println() knows how to print data on the system console (command window). Java’s method names are always followed by parentheses. If you see a method with empty parentheses, this means
that this method does not have any arguments.
The System.out means that the variable out is defined inside the class System that comes with Java. How are you supposed to know that there’s something called out in the class System? Eclipse will help you with this. After you type the word System and a dot, Eclipse
will show you everything that is available in this class. At any time you can also put a cursor after the dot and press Ctrl-Space to bring up a help box similar to this one:

The out.println() tells us that there is an object represented by a variable out and this “something called out” has a method called println(). The dot between a class and a method name means that this method exists inside this class. Say you have a class PingPongGame that has a method saveScore(). This is how you can call this method for Dave who won three games:
PingPongGame.saveScore("Dave", 3);
Again, the data between parentheses are called arguments or parameters. These parameters are given to a method for some kind of processing, for example saving data on the disk. The method saveScore() has two arguments –a text string “Dave”, and the
number 3.
Java Classes:
Java programs consist of classes that represent objects from the real world. Even though people may have different preferences as to how to write programs, most of them agree that it’s better to do it in a so-called object-oriented style. This means that good programmers
start with deciding which objects have to be included in the program and which Java classes will represent them. Only after this part is done, they start writing Java code.
Classes in Java may have methods and attributes.
Methods define actions that a class can perform.
Attributes describe the class.
remote controls and others.

In Java language this class may look like this:

Our class VideoGame should be similar to other classes that represent video games – all of them have screens of different size and color, all of them perform similar actions, and all of them cost money.
We can be more specific and create another Java class called GameBoyAdvance. It also belongs to the family of video games, but has some properties that are specific to the model GameBoy Advance, for example a cartridge type.
In this example the class GameBoyAdvance defines two attributes – cartridgeType and screenWidth and two methods – startGame() and stopGame(). But these methods can’t perform any actions just yet, because they have no Java code between the curly braces.
Data Types:
Remember equations like y=x+2? In Java you’d need to declare the variables x and y of some numeric data type like integer or double:
int x;
int y;
The next two lines show how you can assign a value to these variables. If your program assigns the value of five to the variable x, the variable y will be equal to seven:
x=5;
y=x+2;
In Java you are also allowed to change the value of a variable in a somewhat unusual way. The following two lines change the value of the variable y from five to six:
int y=5;
y++;
Despite the two plus signs, JVM is still going to increment the value of the variable y by one.
After the next code fragment the value of the variable myScore is also six:
int myScore=5;
myScore=myScore+1;
You can also use multiplication, division and subtraction the same way. Look at the following piece of code:
int myScore=10;
myScore--;
myScore=myScore*2;
myScore=myScore/3;
System.out.println("My score is " + myScore);
What this code prints? Eclipse has a cool feature called a scrapbook that allows quickly test any code snippet (like the one above) without even creating a class. Select menus File, New, Scrapbook Page and type the word Test as the name of your scrapbook file.
Now enter these five lines that manipulate with myScore in the scrapbook, highlight them, and click on the little looking glass on the toolbar.

To see the result of the score calculations, just click on the console tab at the bottom of the screen:
My score is 6
In this example the argument of the method println() was glued from two pieces – the text “My score is ” and the value of the variable myScore, which was six. Creation of a String from pieces is called concatenation. Even though myScore is a number, Java is smart
enough to convert this variable into a String, and then attach it to the text My Score is.
Look at some other ways of changing the values of the variables:
myScore=myScore*2; is the same as myScore*=2;
myScore=myScore+2; is the same as myScore+=2;
myScore=myScore-2; is the same as myScore-=2;
myScore=myScore/2; is the same as myScore/=2;
There are eight simple, or primitive data types in Java, and you have to decide which ones to use depending on the type and size of data that you are planning to store in your variables:
- Four data types for storing integer values – byte, short, int, and long.
- Two data types for values with a decimal point – float and double.
- One data type for storing a single character – char.
- One logical data type called boolean that allows only two values: true or false
You can assign an initial value to a variable during its declaration and this is called variable initialization:
char grade = 'A';
int chairs = 12;
boolean playSound = false;
double nationalIncome = 23863494965745.78;
float gamePrice = 12.50f;
long totalCars =4637283648392l;
In the last two lines f means float and l means long.
If you don’t initialize the variables, Java will do it for you by assigning zero to each numeric variable, false to boolean variables, and a special code ‘\u0000’ to a char.
There is also a special keyword final, and if it’s used in a variable declaration, you can assign a value to this variable only once, and this value cannot be changed afterwards. In some languages the final variables are called constants. In Java we usually name final variables
using capital letters:
final String STATE_CAPITAL="Washington";
In addition to primitive data types, you can also use Java classes to declare variables. Each primitive data type has a corresponding wrapper class, for example Integer, Double, Boolean, etc. These classes have useful methods to convert data from one type to another.
While a char data type is used to store only one character, Java also has a class String for working with a longer text, for example:
String lastName="Smith";
In Java, variable names can not start with a digit and can not contain spaces.
A bit is the smallest piece of data that can be stored in memory. It can hold either 1 or 0.
A byte consists of eight bits.
A char in Java occupies two bytes in memory.
An int and a float in Java take four bytes of memory.
Variables of long and double types use eight bytes each.
Numeric data types that use more bytes can store larger numbers.
1 kilobyte (KB) has 1024 bytes
1 megabyte (MB) has 1024 kilobytes
1 gigabyte (GB) has 1024 megabytes


Comments
Post a Comment