Java SE from sketch - Part 2

Java Primitive Data Types:

Data types are to define various types of data in the program to be used, there are 8 primitive data types in java.



Java Variable or Identifier:
  • Variables are to hold different type of data to be used in the program
  • Java is strict case sensitive language
  • Identifiers must start with a letter, a currency character ($), or a connecting
  • character such as the underscore ( _ ). Identifiers cannot start with a number!
  • After the first character, identifiers can contain any combination of letters,currency characters, connecting characters, or numbers.
  • Key words should be ignored to be an identifier
Legal Identifiers:
int _a;
int $c;
int ______2_w;
int _$;
int this_is_a_very_detailed_name_for_an_identifier;

Illegal Identifiers:
int :b;
int -d;
int e#;
int .f;
int 7g;


Java Code conventions:
Sun defined a coding standard and style programmer. Here are the naming standards that Sun recommends as follow,

Classes:
The first letter should be capitalized, and if several words are linked together to form the name, both the words should have first letter uppercases. Classes, the names should typically be nouns like,
System
Account
StudentProfile

Interfaces:
The names should be adjectives like,
Runnable
Serializable

Methods:
The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs like,
getName ()
setFullName()
validateUser()

Variables:
Like methods, the camelCase format should be used, starting with
a lowercase letter like,
maxWidth
accountBalance
formName

Constants:
They should be named using uppercase letters with underscore characters as separators like,
YEAR_RATE
FINAL_VALUE




Writing First Java Program:

Being an OOP language each Java Program has to be written with a class. Let us write a simple program to begin,

public class MyFirst
{
public static void main(String[] args)
{
System.out.println("New Line");
System.out.print("After New Line");
System.out.println("Not in new Line");
}
}
output:
New Line
After New LineNot in new Line


Here we created a public class called MyFirst having a main method,

public static void main(String[] args) {
}

This main method happens to be the starting point of the program and also all other methods will be called from this method.

System.out.println("New Line");

Here System is a built-in class, out is an Object of output Stream and println(); is the method to display data in the console.

Println(); prints data in a line and moves the cursor to new line. Please remember it never starts in a new line.

Print(); print along with current line.

Output of the program should give the complete idea.

NB: about public, static, void and String[] we will discuss soon.

to be continued...

0 comments:

Post a Comment