What are Java primitive data types?

In Java, primitive data types can be categorized within three types:
  1. Integral types:
    • signed integers : byte,short,int and long.
    • unsigned character values, char,denoting all the 65536 characters in the 16-bit Unicode character set.
  2. Floating-point types: float and double, representing fractional signed numbers.
  3. Boolean type: boolean, representing logical values, the two literals, true or false.

A primitive data value can't act as an object in a Java program unless wrapped. Therefore, each primitive data type has a corresponding wrapper class that can be used to represent its value as an object.

What's a Java Literal?

A constant value in a program is denoted by a literal. Literals represent numerical (integer or floating-point), character, boolean or string values.

Example of literals:

Integer literals:
33 0 -9

Floating-point literals:
.3 0.3 3.14

Character literals:
'(' 'R' 'r' '{'

Boolean literals:(predefined values)
true false

String literals:
"language" "0.2" "r" ""

Note: Three reserved identifiers are used as predefined literals:
true and false representing boolean values.
null representing the null reference.



Let's have a closer look at our literals and type of data they can represent:

Integer Literals


Integer data types consist of the following primitive data types: int,long, byte, and short.
int is the default data type of an integer literal.
An integer literal, let's say 3000, can be specified as long by appending the suffix L (or l) to the integer value: so 3000L (or 3000l) is interpreted as a long literal.

There is no suffix to specify short and byte literals directly; 3000S , 3000B,
3000s, 3000b


Floating-point Literals

Floating-point data consist of float and double types.
The default data type of floating-point literals is double, but you can designate it explicitly by appending the D (or d) suffix. However, the suffix F (or f) is appended to designate the data type of a floating-point literal as float.

We can also specify a floating-point literal in scientific notation using Exponent (short E or e), for instance: the double literal 0.0314E2 is interpreted as
0.0314 *10
² (i.e 3.14).

Examples of double literals:

0.0 0.0D 0d
0.7 7D .7d
9.0 9. 9D

6.3E-2 6.3E-2D 63e-1

Examples of float literals:

0.0f 0f 7F .7f
9.0f 9.F 9f
6.3E-2f 6.3E-2F 63e-1f



Note: The decimal point and the exponent are both optional and at least one digit must be specified.


Boolean Literals

As mentioned before, true and false are reserved literals representing the truth-values true and false respectively. Boolean literals fall under the primitive data type: boolean.

Character Literals

Character literals have the primitive data type character. A character is quoted in single quote (').

Note: Characters in Java are represented by the 16-bit Unicode character set.

String Literals

A string literal is a sequence of characters which has to be double-quoted (") and occur on a single line.

Examples of string literals:

"false or true"
"result = 0.01"

"a"

"Java is an artificial language"


String literals are objects of the class String, so all string literals have the type String.



What's are Java keywords?

keywords are reserved identifiers. Predefined identifiers in Java language, that is, they are used in a program exclusively the way already defined by Java. All the keywords are in lowercase. Java compiler enforces this predefined usage. Incorrect usage of keywords if flagged by the compiler and results in compilation errors.

These are the keywords currently defined and used in Java:

abstract default if private this
assert do implements protected throw
boolean double import public throws
break else instanceof return transient
byte enum int short try
case extends interface static void
catch final long strictfp volatile
char finally native super while
class float new switch
continue for package synchronised



Additionally, Java has two reserved keywords which are currently not in use:
const goto

Note: you can't use keywords either used or just reserved by Java.


What's a Java identifier

An identifier is any name in a Java program. Identifiers are used to denote classes, methods, variables and labels.
An identifier in Java can be composed of a sequence of characters, where a character can be either a letter, a digit, a connecting punctuation (such as underscore _ ) or currency symbol (such as $). Anyway, the first character in an identifier cannot be a digit.
Identifiers in Java are case sensitive. For example; car and Car are two different identifiers.

Examples of legal and illegal identifiers in Java:

car is a legal identifier

Car is a legal identifier

be@home contains the character @ which is not legal in an identifier, so the whole identifier is illegal too.By the way @ is not an operator in Java

deja_vu9 is a legal identifier

total_£ is a legal identifier

total-amount contains the character - which is not legal in an identifier, so the whole identifier is illegal too.(however total-amount could be interpreted as legal expression with two operands.)

$_149 is a legal identifier

names99 is a legal identifier

99names is an illegal identifier, it starts with a digit.

لغة is a legal identifier (In Arabic, means "language" if you are too curious :)


Note: Java programs are written in the Unicode character set, so a letter or digit definition is interpreted accordingly.


What's a Java lexical token?

The low level language elements are called lexical tokens (or tokens for short). They are the building blocks for more complex constructs. Identifiers, numbers, operator and special characters are all examples of tokens that can be used to build high-level constructs such as expressions, statements and classes.

Java Class Instantiation

The process of creating objects from a class is called instantiation. So an object is always an instance of a class which represents the blueprint.
The object is constructed using the class and must be created before being used in a program.
Objects are manipulated through object references (also called reference values or simply references)

Creating objects in Java usually follow these steps:

1- Declaration of a reference variable of the appropriate class which will store a reference to the object.
For example:

//declaring my car
Car myCar;

// declaring my father's car
Car myFatherCar ;
Or combined if they belong to the same appropriate class, separated by a comma:

//declaring my car and my father's car
Car myCar, myFatherCar ;

2- Creating an object
This involves using the new operator and calling a constructor to create an instance of the class.
The new operator returns a reference to a new instance of the Car class.
The reference can be assigned to a reference variable of the appropriate class, here: myCar and myFatherCar.

// instantiating myCar from the class Car,
//having the String "black" as parameter value.
myCar = new Car("black");

// instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
myFatherCar = new Car("blue");

Each object has a unique identity and has its own copy of the fields declared in the class.

The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.

The declaration and initialization can be combined:

// declaring and instantiating myCar from the class Car,
//having the String "black" as parameter value.
Car myCar = new Car("black");

// declaring and instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
Car myFatherCar = new Car("blue");

Abstractions, Class and Object. What relation?

Suppose you were asked to describe your lovely car you just bought. You would certainly mention the most important properties according to you.
You would, for instance, stress out the high speed it can reach if you are one of the speedy-cars geeks. Or how spacious it is if you are blessed with one of those large families. Just out-of-university engineer, you would talk about every piece its motor contains and how aerodynamics rules would have been better applied to gain more speed. Or greeny activist, you would just mention the electrical-hybrid motor it contains and its befits for your backyard garden.

you got it :)

Discarding unimportant properties or parts while describing your car, is called abstractions. So, for this simple car example, many abstractions may be done depending on your mechanical skills, visual importance, financial side and many other considerations. Abstraction of the same car differ, hence solving the same problem would practically lead to different solutions.
Abstraction is a tricky and diversion-prone step when developing software.

Java is an Object oriented Programming language(OOP), that means any programming problem can be modeled as one or more objects. So an object models an abstraction by defining the properties and functions making that object different from others. You abstract your car as an object with red-color property and letting you reach you work smoothly each day, as a function. Or as an object being of those luxury Ferari's and having that ghost-oriented shape as properties while operating as a social status need.

let's Java a bit:
Properties of an object are also called attributes and are defined in Java with fields. Functions, also known as operations, are called methods.
Collectively fields and methods in a class are called members.
We may categorize objects to the same class: Car is the class of all cars in our example. nicely this matches the Java naming: class

Let's recapitulate:
Java is an OOP language. A class defines a category of objects. Fields and methods are defined by a class which acts as a footprint for its objects.


Now, let's create our first Java class and give it the name Car:

class Car {

}
class is a Java keyword, it is mandatory and put just before the class name (here, Car). The class scope begin with { and ends with }. They are both mandatory.

From now on I'll write comments within the code itself. // denotes that this code line is a comment. Comments within Java code are for human use and are ignored by the compiler.
class Car { // begin of the class scope

// The constructor. having one String parameter: initialColor
public Car(String initialColor)
{
// Still empty
method body for now.

}
// end of the constructor scope

// Another method with one String parameter: newColor
public void changeColor(String newColor)
{ //begin of changeColor method scope

// Still empty method body for now.

}
//end of this method scope

} // end of the class scope
Car(String initialColor) and changeColor(String newColor) are both Java methods. As with the class, { and } define method's scope.
Generally, a method may have any arbitrary name, by convention, it is a verb or verbal phrase ( ex: changeColor(), startEngin()), followed by its zero or more parameters between enclosing parentheses ( ). Parameters in our example are: String carColor and String newColor.

String is a Java keyword denoting a type. initialColor and newColor are arbitrary parameter names you may choose freely.

As you can see, the method Car(String carColor) has the same name as the class name. In Java, this special method is called a Constructor and is used to create (instantiate, see post) objects from the class.


class Car
{

// carColor variable declaration
String carColor ;

// The constructor method
public Car(String initialColor) {

//
The value of initialColor variable
// is assigned
to carColor variable.
carColor = initialColor ;

}


public void changeColor(String newColor )
{


// carColor variable is assigned the value of newColor
carColor = newColor;

}

}

In this class, we declare one instance field. We choose its name to be initialColor denoting a variable that will hold the car color value. Its type is String.

String carColor; is called in Java a declaration. The semicolon at the end is mandatory indicating the end of the declaration;
Generally, you may declare as many fields as you wish.

see u next time :)


Which tool shall I use for java?

To develop Java programs, one option is using a tool that contains at least:
  1. A text editor so that you can write code
  2. A compiler to compile and detect errors in your code.
  3. Runner so that you can execute your code and run the resulting program.

A tool with such built-in capabilities is called Integrated Development Environment (IDE).

There are many IDE's out there, either open source (free) or paid. To name some: Eclipse, NetBeans,JCreator, JBuilder,IntelliJ.

I personally experienced many. As for now, further comparing the most common ones is beyond the scope of this tutorial.

There is a wealth of information on how to install, configure and use your chosen IDE in its respective website.
So to avoid recreating the wheel I briefly show the road-map and let you learn yourself by practicing.

Eclipse IDE:

  1. Download and install a Java Runtime Environment (JRE 5.0 Update 9, as for now)
  2. Download and unzip Eclipse SDK package: http://www.eclipse.org/downloads/
  3. To run Eclipse IDE simply run the included eclipse.exe or eclipse executable in the unzipped directory.

For advanced configuration and related topics: Eclipse FAQ.


NetBeans IDE, another powerful IDE, can be separately downloaded from:
http://www.netbeans.info/downloads/download.php?type=5.0

But I strongly suggest to download the bundled file (JDK 5.0 Update 9 with NetBeans IDE 5.0 Bundle) from:
http://java.sun.com/j2se/1.5.0/download-netbeans.html

This way you get NetBeans IDE and the needed J2SE as a bundle so that you don't need to download and install them separately.


What is a programming language?

As its name denotes. It is an artificial language and has some similarities with natural languages. The most common factor is grammar.
So, to write some sentences in a natural language (Arabic, English,..), you need to know how to form sentences according to its grammatical rules.
And to write some lines (code) in a programming language, let's say, Java, you should write according to Java grammatical rules.

Learning how to program is learning how to use those rules correctly.

So a programming language is the tool to make a computer perform our instructions.

To break the icy words, here is a snapshot of code written in Java programming language:

public static void main(String args[]){
System.out.println("Hello World!");
}


This sample code follows Java rules. It instructs a computer to print out "Hello World!"

But, a real-world program would certainly need more code lines.
.. not yet lost :)
Well then, don't care too much if it is still messy. We all went through once. I'm just trying to make you familiar with the concept of programming.

Detailed and step by step tutorials will follow.