Search Java Tutorials

Custom Search

What You Will Want to Complete This Tutorial?

You must have at least 1st and 2nd pre-requirements to start the tute. Other requirements will require for last chapters only. These all are free to use and download!
  1. Java Development Kit Standard Edition (JDK / J2SE) Any Version(www.java.com)
  2. Any Text Editor (Note Pad quite enough or get Notepad++ from (http://notepad-plus.sourceforge.net)
  3. Java Run Time Environment (JRE) Any Version(www.java.com)
  4. Java Enterprise Edition (J2EE) for advanced developing (For last few chapters)(www.java.com)
  5. Java Wireless Toolkit for Mobile Application Development (For Mobile Application Development Chapters) (www.java.com)
  6. Tomcat Web Server to run Servlets. (www.tomcat.apache.org/)
Your operating system may be any version of Windows.
What will you learn (As a summary)?
  1. How to build console and windowed applications using Java
  2. How to build web page elements with Java Applets
  3. How to build Database Programs with Java
  4. How to build enterprise applications with Beans
  5. How to add Email functions to Java programs
  6. How to program smart devices (Phone, PDA etc…) with Java

Java Variable Declaration

Variables are named memory locations that your program can use to store values. Line 5 declares our variable “strCountry” as follows.

String strCountry;

“String” is the data type of the variable. That means this variable can hold texts, numbers or set of different characters.

“strCountry” is the name of the variable and known as variable identifier.

Other than strings, you may want to declare variables that can hold only numbers, but not characters; or variables that can hold images. Java provides eight primitive variable types and three reference variable data types. (Primitive variable types are the building blocks of Reference data types.)

The primitive types (also known as bulletin data types) comprise of all the numeric types along with a boolean type. The numeric types are four integral types- byte, short, int, long, andchar, and two floating-point types- float and double.

Primitive Data Type

Usage

Capacity

Default Value

Maximum Value able to hold

Minimum Value able to hold

byte

Sign integer

1 byte

0

127

-128

char

Single character

1 byte

u\0000

-

-

short

Sing integer

2 bytes

0

32, 767

-32,768

boolean

True or false

2 bytes

false

-

-

int

Sign integer

4 bytes

0

232-1

-232

float

Floating point values

4 bytes

0.0f

3.4*1038-1

-3.4*1038

long

Sing integer

8 bytes

0L

264-1

-264

double

Floating point values

8 bytes

0.0

1.7*10308-1

-1.7*1-308


The variables of a reference type hold thereferences to the actual objects.

Reference Data Type

Usage

Capacity

Default Value

Maximum

Minimum

Array

Object


Variable Naming Rules

  1. Variable name must be start with a letter, doller sign or underscore.
  2. No embedded space characters allowed within variable name.
  3. Cannot use keywords as variable names.

You can use access modifies like “private” with variable declaration. Default access modifier in “public” that means if you haven’t declare a access modifier, then java will assume it as a “public” variable.

Ex: private String strCountry;

Variable names usually start with lowercase letters to distinguish them from class names.

You can do the variable declaration and assigning values in one statement.

Ex: String strCountry = “India”;

Also be aware how to indicate assignment of value. String variables need its assigning value within “”. Integer, double, float, long, short, byte need value directly. Char needs its assigning values within ‘’.

Ex:

String strCountry = “Sri Lanka”;

Int intCountryCode = 94;

Char chrCountryLetter = ‘S’;

Every text you input using the keyboard into the program is receives as a String. And every thing you print to screen must be a string. You can print an integer on screen using the statement (If x is an integer): System.out.println(x);. Computer will convert integer ‘x’ into String data type and then prints on screen.

We’ll discuss about reference types later.

Tip: Java is a strongly typed language, which means that every variable and expression has a type that is known at compile-time itself

SCJP Tip: Variables declare inside the class declaration, but not inside a method known as ‘class data fields’ or ‘member variables’. Then variables declare inside methods known as ‘local variables’.

SCJP Tip: There’re two kinds of member variables. (1)The static variable is a member variable declared using the keyword static within a class body. Static variables are also known as class variables. They are created when the class is loaded. (2) The instance variable is also a member variable, but it is declared without the keyword static. Instance variables come into life when an instance (i.e. object) of a class is created and they remain associated with that instance. Instance variables are destroyed as soon as that particular instance is destroyed.

No comments: