Thursday, January 22, 2009

Is a "main" method required for every class in Java?

Welcome back to Java Code Online. One of the most basic conception for a fresher is to put a main method in each Java class he or she writes. Most often they never know that, not all Java Class need a main method.

Java is an Object Oriented language based on the Principle of OOPS. The basic principle of OOPS is to visualize everything as an Object. The best coding practice is to make a separate class for each Object that is required by your Java Code.

The "main" method is required to start the program. Whenever the Java program is executed, the JVM looks for the method containing the "main" method. It gets it in the form of public static void main(String args[]). This method tells the JVM that ok, I am the main method and you need to start reading the code from me.

There could be multiple classes with different methods serving different functionality, though none of them may contain a "main" method. There could be a single class which controls or more specifically uses these other classes, make their Objects and use them in the Java Code.

So, now I think that I am clear. It is not compulsory for every class in Java to have a "main" method, though there has to be class with a main method which uses uses these classes.

I hope the article was beneficial in understanding the requirement of the "main" method in the Java Code. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Wednesday, January 21, 2009

Difference between Sysyem.out.println and System.out.print in Java

Welcome back to Java Code Online. System.out.print and System.out.println are used to print the output at the console. It is one of the most commonly used command in Java.

Often while coding we come across System.out.print and System.out.println. Have you ever wondered what could be the possible difference between these two Java Commands. I will explain it to you:-
1. System.out.print prints the output in the same line.
2. System.out.println inserts a new line there by enhancing the beauty of the output.

println of System.out.println is normally interpreted as print new line. So you can remember it easily. If you want that your Java code output appears in a new line each time, then use System.out.println. But if you want that all the Java Code output should continue coming on the same line, then use System.out.print.

I hope the article clarifies your doubt about System.out.print and System.out.println. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Why is everything in Java written in a class

Welcome back to Java Code Online. We all know that Java is an Object oriented language based on the Principle of OOPS. The basic principle of Java is to recognize your Objects. Whenever you need to start coding, just ask yourself, what are the Objects involved in this Java code.

Now once you have finalized your Objects, then you need to put them in code. But if you start putting all your Object code in a single large Java file, then it will look very unclean and messy, moreover the beauty of the code is lost. What best can be done is that we put the code in classes.

A class is a blueprint of an Object, so it define the Object. So as a natural process of coding in Java, you recognize your Objects put them in separate classes and then write a main class that make Objects of those classes and perform the operations required.

It is simply equivalent to the human body, where the main class is the brain of our human body, we have limbs, legs, fingers, joints, heart, etc. each performing its own function. So it is like all the parts are Objects in Java for which we write separate classes, now these separate classes are used by the main class (brain of the human body), and so we see that all the parts coordinate and perform the tasks they are made for. Simple.

If you really want to understand the basics of Java, then do often relate it to practical life things, and you will find Java very easy, this is because Java is built to work on practical situations.

I hope the article answered the question that why everything in Java is written in a class. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Sunday, January 11, 2009

What is the difference between instance variable and local variable

Welcome back to Java Code Online. While working with Java, you must have multiple times come across words like instance variable and local variable. But have you ever wondered that what is the difference between them. Well, today I am going to discuss that. But before proceeding on this topic you need to know what is an instance variable and what is a local variable in Java. Refer to my previous post for information on the instance variable and local variable in Java.

There are two major differences between an instance variable and a local variable in Java. They are:-

1. Instance variable is declared within a class but not within a method, while a local variable is declared within a method.

2. Instance variable get a default value, so if they are used before initializing them, then also they compile fine. But in the case of local variable in Java, local variable in Java do not get an initial value, so if you use them in your code without initializing them, then you will get a compile time exception.

I hope the article was helpful in understanding the difference between instance variable and local variable in Java. If you liked the article, then do leave a comment. For more information on Java keep buzzing Java Code Online.

Saturday, January 10, 2009

What is an instance variable in Java

Welcome back to Java Code Online. You must have heard the instance variable a multiple number of times while writing your Java code, but do you really understand what is a instance variable in Java. Today I will discussing about instance variables in Java.

An instance variable in Java is one which is declared within a class and not within a method. Whenever you write code in Java, you do it with the help of classes. In most of the classes you need to declare some variables for some purpose or the other. These variables which are declared within the class but not within the method are called instance variables in Java.

If you declare the variable within a method, then it becomes a local variable of that method and not an instance variable of the class. So you need to declare it within a class but outside any method. I will clarify my point with an example.

class JavaCode{
int code;
float test;
//Some Java Code here
}

In the above Java Code, the variables "code" and "test" are local to a class but are outside any method, and are therefore instance variables of this class. I hope you got my point now.

One important point about instance variable is that, they always get a default value. If you declare any instance variable in your Java Code and use them without initializing them, then also the compiler will not complain, and it will compile properly. This happens because the compiler assigns the instance variables a default value. So if the instance variables are not initialized then the default value is used in their place.

The default values of the various type of variables is shown below:-
integers-----------0
floating points----0.0
booleans-----------false
references---------null

These are default values which are assigned to various types of variables. So to summarize the whole thing.
1. instance variables are declared inside a class but not within a method.
2. instance variables always have a default value assigned to them.

I hope this article helped you to understand what is an instance variable in Java. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Friday, January 9, 2009

What is a local variable in Java

Welcome back to Java Code Online. You must have used local variables a multiple number of times in your Java Code. Today we will discuss what actually is a local variable in Java.

A local variable in Java is one which is defined in a "method". Whenever you write your code, you make methods to do things in Java. Then you call those methods and perform the necessary operations. Any variable that is declared within a method is called a local variable.

I will take an example here:-

class JavaCode{
public int code(){
int codeLength = 20;
System.out.println("The Java Code length is:-"+codeLength);
return codeLength;
}
}

This is simple Java Code for declaring a class called JavaCode with a method called code(), which returns an integer. Here you can see that the variable "codeLength" is declared within method and is therefore a local variable.

Whenever you have to judge that a variable is local or not in Java, just ask yourself, that is the variable local to a method or not. If a variable is defined in a method or is local to a method, then it is a local variable otherwise not.

One more very important feature of local variable in Java is that they do not have an initial value. So if you declare a local variable in your code and do not initialize it, then that local variable can not be used. Mark my words here, if you use a local variable in Java before initializing it then it will give a compile time exception.

So to summarize the whole thing, there are two properties of a local variable in Java. They are:-
1. A local variable must be defined within a method.
2. A local variable must be initialized before using it in your code.

I hope the article helped you understand what is a local variable in Java. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

Thursday, January 8, 2009

What is a variable in Java

Welcome back to Java Code Online. Today I will be discussing about what is a variable in Java. In Java, whatever you do, you need to do that with the help of variables.

A variable as the name signifies is something that can vary. I do not mean that it will always vary, there are modifiers in Java like "final" which if used along with the variable, makes it impossible to vary. A variable is like a container, which has the capability to hold something.

You remember that when you are feeling sleepy, then your mom gives you tea. The tea is in a cup or glass. This cup or glass is a container, that holds the tea. Similarly in Java, the variable which is like a container holds some value or some reference parameter.

A variable in Java could be of two types:-
1. primitive variable
2. object reference variable

Primitives variables are used to hold basic values needed for normal computation bu Java, like some number, or an integer or a floating point number or it could be a boolean also. Object Reference variables are used to reference to some Object.

A variable has to follow some rules, in order to qualify as variable in Java. The rules are simple and must always be implemented. The rules are:-
1. A variable must have a type.
2. A variable must have a name.

I will clarify on this point with an example. If you say:-

float marks;

Then in the above Java code, "float" is the type of variable and "marks" is the name of the variable. Simple isn't it.

I hope the article helped you in understanding what is a variable in Java. If you liked the article then do leave a comment. Foe more info on Java, keep buzzing Java Code Online.

Friday, January 2, 2009

Hello World Program in Java

Hello all, Welcome back to Java Code Online. Today I will start with a very simple Java Code program. This is called the Hello World Program, and when executed prints "Hello World" at the output.

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

This program needs to be saved with the name of MyFirstCode.java, because in Java the name of the class containing the main method, needs to be the name of the java file itself.

When you execute this code, then you will get the output as "Hello World".

So, enjoy the world of Java, and make your own experiments with coding. See you later guys and gals. Keep buzzing Java Code Online.

Thursday, January 1, 2009

Character Escape Sequence in Java

Welcome back to Java Code Online. The character escape sequence in Java is highly useful when dealing with Strings. These escape sequence help in formatting the output as required. This gives the developer a greater control over the text and the output.

For example suppose that you need to print a String "Hello, I am Learning Java". Then the code for this will be:-

String str = "Hello, I am Learning Java";

Now suppose that you want to enclose Java in double quotes, like you want the output to be something like:-

Hello, I am Learning "Java"

For this you need to place double quotes withing the String statement. But oops, there is a problem, if you put double quotes in a String statement, then it will break the String. I mean, if you say that:

String str = "Hello, I am Learning "Java"";

Now this will create blunder as in Java the declaration of a String is done in double quotes, so Java will assume that the statement starting from the first double quote is the starting point of the String, and the second double quote will close the String. But this approach will leave the word "Java" outside the String declaration.

Java comes with a solution to that, here the Character Escape Sequence of Java comes to rescue. The escape sequence allows you to embed special characters like double quotes, line feed, carriage return, tab, etc. into the same String. The above example is corrected as follows:-

String str = "Hello, I am Learning \"Java\"";

Now this is the correct way as per Java. What Java sees is that anything after the "\" symbol is to be placed as it is, and is a part of the whole String overall. Though the symbol "\" has different implications with regard to other escape sequences, like "\n" inserts a new line, etc.

The Character escape Sequence in Java is shown below:-

Escape Sequence ------------Purpose
\ddd ------------ Octal character (ddd)
\uxxxx ------------ Hexadecimal UNICODE character (xxxx)
\’ ------------ Single quote
\” ------------ Double quote
\\ ------------ Backslash
\r ------------ Carriage return
\n ------------ New line (also known as line feed)
\f ------------ Form feed
\t ------------ Tab
\b ------------ Backspace

I hope the article was helpful in understanding the Character Escape Sequence in Java. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.