Welcome back to Java Code Online. Boolean Data Type is one of the most useful data type, when you need some sort of definitive action. Like it has to be a yes or no, nothing in between. It is exactly like binary, where the numbers could be 0 or 1, nothing else. Boolean Data type is one of the four groups of data types in Java which are Integer Data Type in Java, Floating Point Data Type, Character Data Types and the fourth is Boolean Data type itself.
The Boolean data type tests a particular condition or expression, and evaluates that the condition is "true" "false". The true and false are in quotes because these are the values which are returned while using a Boolean Data Type in Java.
In the Boolean Data type Category, there is only a single data type called:-
1. boolean (true or false)
boolean
The boolean data type tests a logical condition, and returns either true or false. The number of bits that a boolean variable occupies is JVM specific. Only two values are possible that are "true" or "false". The boolean data type is used extensively by the relational operators as the return type is always boolean, and also in loops. I will cover loops in Java later. For example:-
boolean b;
Then it is implicit that this variable "b" can have only two possible values and they are "true" or "false". Boolean data type are very handy while testing conditions.
I hope the article helped you get a groove on the Boolean Data types in Java. If you like this article then do post a comment. For more info on Java, keep buzzing Java Code Online.
Wednesday, December 31, 2008
Tuesday, December 30, 2008
What are Character Data Types in Java
Welcome back to Java Code Online. The data types in Java can be categorized in four categories. Character Data Types is the third category after Integer Data Types and Floating Point Data Types.
In Java the Character Data type is of single type. It is:-
1. char (16 bits) (range: 0 to 65535)
We will discuss this data type of Java in detail now.
char
"char data type in Java" has some special features which were not present in its predecessors like C and C++. In Java the "char" Data Type is 16 bit long. Java uses Unicode to represent characters, and so for languages which have long Unicode representations do have characters which require more then 8 bit of data. That makes Java a truly international language, since by using Unicode charset, Java can define any language in the world.
If you have noticed above, then you would have seen that the range of "char" data type is from 0 to 65535, there are no negative chars. The range that the "char" data type enjoy in Java, is truly awesome, and makes Java one of the most global languages in the world.
Regarding functionality of the "char" data type. A "char" data type in Java is capable of handling one character at a time. For example:-
char testing = 'j';
Here the variable testing is of "char" Data Type, and is assigned equal to the character 'j'. So isn't it simple.
Now the Character Data types in Java must be clear to you. In case you liked my article or if if you have any doubt, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
In Java the Character Data type is of single type. It is:-
1. char (16 bits) (range: 0 to 65535)
We will discuss this data type of Java in detail now.
char
"char data type in Java" has some special features which were not present in its predecessors like C and C++. In Java the "char" Data Type is 16 bit long. Java uses Unicode to represent characters, and so for languages which have long Unicode representations do have characters which require more then 8 bit of data. That makes Java a truly international language, since by using Unicode charset, Java can define any language in the world.
If you have noticed above, then you would have seen that the range of "char" data type is from 0 to 65535, there are no negative chars. The range that the "char" data type enjoy in Java, is truly awesome, and makes Java one of the most global languages in the world.
Regarding functionality of the "char" data type. A "char" data type in Java is capable of handling one character at a time. For example:-
char testing = 'j';
Here the variable testing is of "char" Data Type, and is assigned equal to the character 'j'. So isn't it simple.
Now the Character Data types in Java must be clear to you. In case you liked my article or if if you have any doubt, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Monday, December 29, 2008
What are Floating Point Data Types in Java
Welcome back to Java Code Online. As I have promised in my previous article, that i will discuss all the data types in Java. So here I am back again, with the second data type in Java known as the Floating Point Data Types. Floating point data Types are useful when dealing with fractional values and are actually linked with the "real" numbers. This is unlike Integer Data Type in Java which are linked to "whole" numbers.
Floating Point Data Types in Java are of two types. they are:-
1. float (32 bits)
2. double (64 bits)
I will discuss both of these data types in detail below.
float
"float" data type takes 32 bits, and is single precision. Since the value of precision is lower then the "double" data type, the "float" data type occupy less memory as compared to the "double" data type. Moreover the processing of "float" is comparatively faster as compared to a "double" data type in Java.
The downside of using "float" is that, the precision is smaller then "double". So very small and very large values cannot be stored in a "float". Apart from that "float" meets all the requirements for a normal coder.
double
"double" consumes 64 bit of storage and is double precision. This data type is most used for scientific calculations, or places where very high degree of precision is required by the application.
I hope the article helped you understand about Floating Point Data Types in Java. If you liked this article then do leave a comment. For more info on Java keep buzzing Java Code Online.
Floating Point Data Types in Java are of two types. they are:-
1. float (32 bits)
2. double (64 bits)
I will discuss both of these data types in detail below.
float
"float" data type takes 32 bits, and is single precision. Since the value of precision is lower then the "double" data type, the "float" data type occupy less memory as compared to the "double" data type. Moreover the processing of "float" is comparatively faster as compared to a "double" data type in Java.
The downside of using "float" is that, the precision is smaller then "double". So very small and very large values cannot be stored in a "float". Apart from that "float" meets all the requirements for a normal coder.
double
"double" consumes 64 bit of storage and is double precision. This data type is most used for scientific calculations, or places where very high degree of precision is required by the application.
I hope the article helped you understand about Floating Point Data Types in Java. If you liked this article then do leave a comment. For more info on Java keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Sunday, December 28, 2008
What are Integer Data Types in Java
Welcome back to Java Code Online. Integer Data types are the most commonly used data types in Java. They provide the developers with an extraordinary power to deal with numbers. The Integer data types are the ones which deal with whole numbers.
The integer data types in Java is of four types. They are :-
1. byte (8 bits) (range: -128 to 127)
2. short (16 bits) (range: -32768 to 32767)
3. int (32 bits) (range: -2147483648 to 2147483647)
4. long (64 bits) (range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
The order is from smallest to largest. All the Integer Data types in Java are signed numbers, and they could be positive or negative. The above order depicts all the key features of the four Integer data types. I will cover each of them in detail below.
byte
This is the smallest of all the Integer Data Types consisting of 8 bits. These are signed 8 bit data types used for handling very small amount of data. As you can see from the above rankings, they can store up to a maximum of 8 bits, or the highest decimal possible is 127, i.e 2^7 - 1. Here 7 is used for power because one bit is reserved for the positive or negative sign., so it leaves it with 7 bits.
As the value which could be hold is very small, they are not much used in applications, apart from handing some binary operations where the data range is very small.
short
"short" data type comes after the "byte" data type in the hierarchy, they are 16 bit signed data types. These data types are the least used data types among all the various data types in Java.
int
After "short" data type comes the most used and most common data type in Java, and that is "int". The point that makes it the most versatile data type in Java, is the fact that its range covers almost all the possible numbers which could be used by a common person. The rankings above show that in case of an "int", the range is large enough to hold any kind of number.
long
"long" is the last data type in the rankings and comes after "int" data type. The range that a "long" can accommodate is huge, and is infact only used in those rare occasions when the int is not sufficient to hold the number. This data type is quite useful when dealing with very large numbers.
I hope the article helped you understand about the Integer Data types in Java. If you liked this article, then do leave a comment. For more information on Java, keep buzzing Java Code Online.
The integer data types in Java is of four types. They are :-
1. byte (8 bits) (range: -128 to 127)
2. short (16 bits) (range: -32768 to 32767)
3. int (32 bits) (range: -2147483648 to 2147483647)
4. long (64 bits) (range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
The order is from smallest to largest. All the Integer Data types in Java are signed numbers, and they could be positive or negative. The above order depicts all the key features of the four Integer data types. I will cover each of them in detail below.
byte
This is the smallest of all the Integer Data Types consisting of 8 bits. These are signed 8 bit data types used for handling very small amount of data. As you can see from the above rankings, they can store up to a maximum of 8 bits, or the highest decimal possible is 127, i.e 2^7 - 1. Here 7 is used for power because one bit is reserved for the positive or negative sign., so it leaves it with 7 bits.
As the value which could be hold is very small, they are not much used in applications, apart from handing some binary operations where the data range is very small.
short
"short" data type comes after the "byte" data type in the hierarchy, they are 16 bit signed data types. These data types are the least used data types among all the various data types in Java.
int
After "short" data type comes the most used and most common data type in Java, and that is "int". The point that makes it the most versatile data type in Java, is the fact that its range covers almost all the possible numbers which could be used by a common person. The rankings above show that in case of an "int", the range is large enough to hold any kind of number.
long
"long" is the last data type in the rankings and comes after "int" data type. The range that a "long" can accommodate is huge, and is infact only used in those rare occasions when the int is not sufficient to hold the number. This data type is quite useful when dealing with very large numbers.
I hope the article helped you understand about the Integer Data types in Java. If you liked this article, then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Saturday, December 27, 2008
What are Java Data Types
Welcome back to Java Code Online. You will come to know that Java care about its variables, and would not allow a larger value in a smaller variable. Every variable in Java can be classified as:-
1. Primitive
2. Reference
The data types in Java are concerned with the Primitive type variables, and I will only discuss these in this article. In my later article I will discuss about the reference variables.
In Java, we need variables to store values and perform operations, for these they need to be assigned some sort of data type. A Data Type defines the intrinsic property of the variable, like its range, the maximum and minimum value it can hold, etc. A correct data type need to be used for every operation in Java.
Java supports eight Data Types which could be grouped in four domains. They are:-
1. Integers (byte, short, int, long)
2. Floating Point Numbers (float, double)
3. Characters (char)
4. Boolean (boolean)
The names in the bracket are the actual Data Types used in Java. For example for a mathematical operation, most probably an Integer type or Floating type of data variable will be required. There might be values which are very large and may need a long or double data type. Similarly for a logical condition giving output as 1 or 0, or could be true or false needs to be assigned a Boolean data type. So we see that assigning a correct Data Type is essential for the correct operation of the Java Code.
I would be covering all these four data types in my next consecutive articles. I will also discuss, why and where to use which type of Data Type. And the most important thing is that why not to use the data type with the largest range if it is not required. As it is commonly seen, that while performing a mathematical operation, we assign the Data Type with the largest range, so as to avoid any range overflow. But this is a bad practice. We should be aware of, that where to use which type of Data Type and where not to.
I hope this article gave you a good glimpse of Java Data Type. If you liked this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
1. Primitive
2. Reference
The data types in Java are concerned with the Primitive type variables, and I will only discuss these in this article. In my later article I will discuss about the reference variables.
In Java, we need variables to store values and perform operations, for these they need to be assigned some sort of data type. A Data Type defines the intrinsic property of the variable, like its range, the maximum and minimum value it can hold, etc. A correct data type need to be used for every operation in Java.
Java supports eight Data Types which could be grouped in four domains. They are:-
1. Integers (byte, short, int, long)
2. Floating Point Numbers (float, double)
3. Characters (char)
4. Boolean (boolean)
The names in the bracket are the actual Data Types used in Java. For example for a mathematical operation, most probably an Integer type or Floating type of data variable will be required. There might be values which are very large and may need a long or double data type. Similarly for a logical condition giving output as 1 or 0, or could be true or false needs to be assigned a Boolean data type. So we see that assigning a correct Data Type is essential for the correct operation of the Java Code.
I would be covering all these four data types in my next consecutive articles. I will also discuss, why and where to use which type of Data Type. And the most important thing is that why not to use the data type with the largest range if it is not required. As it is commonly seen, that while performing a mathematical operation, we assign the Data Type with the largest range, so as to avoid any range overflow. But this is a bad practice. We should be aware of, that where to use which type of Data Type and where not to.
I hope this article gave you a good glimpse of Java Data Type. If you liked this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Friday, December 26, 2008
What is Java Virtual Machine(JVM)
Welcome back to Java Code Online. Java Virtual Machine(JVM) is a program specific to Java used for executing Java Bytecode. The compiler converts the Java Source Code into Java bytecode.
The main advantage of JVM is that, once a code is written and compiled on any platform, then it could be executed on the same on any other platform. The only requirement is that JVM must be installed on that device. JVM accepts standardized binary code. So if a developer can write Bytecode directly, then he does not need a compiler. He can directly run the bytecode using JVM.
But the developer writing bytecode is totally absurd, no developer how much talented he is can write directly bytecode. Java Bytecode is highly optimized binary code understood only by the JVM.
So the main point that comes out is the Sun Microsystems slogan that "write once and run anywhere". JVM provides Java with a specifically great advantage of security and portability. The security feature comes along due to the fact the JVM is like an enclosed shell, which encloses everything regarding execution of the program, so nothing goes out of JVM. And thus the threat of affecting the system by the Java Code is removed.
Portability comes along due to the fact that, once you have written and compiled the code, you can carry the Bytecode file anywhere and run it, the only requirement is that JVM should be present on that device you internet to run your bytecode. And regarding the current scenario, Java is everywhere and so is JVM.
I hope the article was helpful in explaining JVM properly to you. If you like the article then do leave a comment. For more information on Java keep buzzing Java Code Online.
The main advantage of JVM is that, once a code is written and compiled on any platform, then it could be executed on the same on any other platform. The only requirement is that JVM must be installed on that device. JVM accepts standardized binary code. So if a developer can write Bytecode directly, then he does not need a compiler. He can directly run the bytecode using JVM.
But the developer writing bytecode is totally absurd, no developer how much talented he is can write directly bytecode. Java Bytecode is highly optimized binary code understood only by the JVM.
So the main point that comes out is the Sun Microsystems slogan that "write once and run anywhere". JVM provides Java with a specifically great advantage of security and portability. The security feature comes along due to the fact the JVM is like an enclosed shell, which encloses everything regarding execution of the program, so nothing goes out of JVM. And thus the threat of affecting the system by the Java Code is removed.
Portability comes along due to the fact that, once you have written and compiled the code, you can carry the Bytecode file anywhere and run it, the only requirement is that JVM should be present on that device you internet to run your bytecode. And regarding the current scenario, Java is everywhere and so is JVM.
I hope the article was helpful in explaining JVM properly to you. If you like the article then do leave a comment. For more information on Java keep buzzing Java Code Online.
Wednesday, December 24, 2008
What is Java Source Code
Welcome back to Java Code Online. Java Source Code is simply the code you write in Java. There are many editors available right now for writing Java Code. The most simple editor for writing Java Source Code is the "Notepad" provided by Windows.
There are many pioneers in Java Technology, in industry major of the Java applications are written in Eclipse. One more tool that is becoming very famous recently is WSAD(Web Sphere Application Developer) by IBM. This tool is really good.
I have been using this tool for a long time now, and believe me it meets all your requirements for Java coding. The tools provided in WSAD are awesome. If you have a chance then do put your hands on this tool. WSAD is built on Eclipse itself, so having a knowledge of Eclipse will be very beneficial in using WSAD for writing Java Source Code.
By default the Java Source Code files have .java extension. So that's it for now guys. If you liked this article then do leave a comment. For more info on Java keep buzzing Java Code Online.
There are many pioneers in Java Technology, in industry major of the Java applications are written in Eclipse. One more tool that is becoming very famous recently is WSAD(Web Sphere Application Developer) by IBM. This tool is really good.
I have been using this tool for a long time now, and believe me it meets all your requirements for Java coding. The tools provided in WSAD are awesome. If you have a chance then do put your hands on this tool. WSAD is built on Eclipse itself, so having a knowledge of Eclipse will be very beneficial in using WSAD for writing Java Source Code.
By default the Java Source Code files have .java extension. So that's it for now guys. If you liked this article then do leave a comment. For more info on Java keep buzzing Java Code Online.
Tuesday, December 23, 2008
What is Java Compiler
Welcome back to Java Code Online. Understanding the functionality of the Java Compiler helps in understanding the processing at the back end of Java. A Java Compiler compiles the source code in Java and converts it into Bytecode which is used by the JVM in running the application.
In case of most of the programming languages like C, the compiler compiles the source code straight into executable code. This has the drawback of effecting the system configuration or programming viruses that can affect the functionality of the system.
Java has a two tier architecture that helps in avoiding the above mentioned issue. In Java, the source code is converted into Bytecode by Compiler, this Bytecode is only understood by the JVM. And then the JVM runs this bytecode. So anyhow nothing can go out of the shell of the JVM(Java Virtual Machine). The main advantage of this is security.
You can develop really secure applications in Java, without the fear that it may affect the system's functionality. Moreover any device like mobile or ipod or any other electronic gadget can use mostly use Java application without the fear of it being a virus. This and some other features of Java make it real strong and robust for any sort of conditions.
The functionality of the Java Compiler comprises of checking any violations of the syntax, and catching any exceptions that may occur during the execution of the program. Only run time exceptions are not checked during compilation of the Java Code by the Java Compiler. This is because Java wants to add an extra feature called "dynamic binding" which occurs at runtime, this concept comes under polymorphism, and I will discuss it again in the article for polymorphism. The comiler provided by Java is called "javac".
The "dynamic binding" provides the Java Code flexibility to bind Objects at runtime, that is the developer also does not know which Object will bind during the execution of the program. This is one of the most powerful concepts of Java.
I hope that this article help you understand the Java Compiler. In case you liked the article, do leave a comment. Remember to check my next article regarding Java on Java Code Online again.
In case of most of the programming languages like C, the compiler compiles the source code straight into executable code. This has the drawback of effecting the system configuration or programming viruses that can affect the functionality of the system.
Java has a two tier architecture that helps in avoiding the above mentioned issue. In Java, the source code is converted into Bytecode by Compiler, this Bytecode is only understood by the JVM. And then the JVM runs this bytecode. So anyhow nothing can go out of the shell of the JVM(Java Virtual Machine). The main advantage of this is security.
You can develop really secure applications in Java, without the fear that it may affect the system's functionality. Moreover any device like mobile or ipod or any other electronic gadget can use mostly use Java application without the fear of it being a virus. This and some other features of Java make it real strong and robust for any sort of conditions.
The functionality of the Java Compiler comprises of checking any violations of the syntax, and catching any exceptions that may occur during the execution of the program. Only run time exceptions are not checked during compilation of the Java Code by the Java Compiler. This is because Java wants to add an extra feature called "dynamic binding" which occurs at runtime, this concept comes under polymorphism, and I will discuss it again in the article for polymorphism. The comiler provided by Java is called "javac".
The "dynamic binding" provides the Java Code flexibility to bind Objects at runtime, that is the developer also does not know which Object will bind during the execution of the program. This is one of the most powerful concepts of Java.
I hope that this article help you understand the Java Compiler. In case you liked the article, do leave a comment. Remember to check my next article regarding Java on Java Code Online again.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Monday, December 22, 2008
What is Java Package
Welcome back to Java Code Online. Java Package is a very convenient way of grouping similar classes, or classes having similar functionality. Java is a pile of classes, innumerable if you go on reading one by one. It would be impossible to remember every class, just by the name of the class. So what Java does is that group them together into packages.
Java consists of many packages, each serving some or the other functionality. Whenever you need to use some particular class from a package, you need to import the package. There is another way though, you can use the complete name of the class including the package name every time you need to use that class. But believe me, that is truly ridiculous. It is always better to import the package in which the class is present.
For example you need the ArrayList class, then there are two ways of using this class in your java code. They are:-
1. import java.util.ArrayList;
2. java.util.ArrayList al = new java.util.ArrayList();
The first way is the normal and regular way of importing a Java Package in a Java Class. If you do not want to go by the first way, then there is a more tedious way of doing the same thing. The second way demonstrates this thing, here you use the full qualified name of the class required. The problem with this second way is that, here every time you need to use the full qualified name of the Java Class required. That is a bit more tiring as compared to the first and easier way of importing the package once, and that's all that is required.
So now you must have a good understanding about Java Package. If you like the article then do leave a comment and keep learning Java at Java Code Online.
Java consists of many packages, each serving some or the other functionality. Whenever you need to use some particular class from a package, you need to import the package. There is another way though, you can use the complete name of the class including the package name every time you need to use that class. But believe me, that is truly ridiculous. It is always better to import the package in which the class is present.
For example you need the ArrayList class, then there are two ways of using this class in your java code. They are:-
1. import java.util.ArrayList;
2. java.util.ArrayList al = new java.util.ArrayList();
The first way is the normal and regular way of importing a Java Package in a Java Class. If you do not want to go by the first way, then there is a more tedious way of doing the same thing. The second way demonstrates this thing, here you use the full qualified name of the class required. The problem with this second way is that, here every time you need to use the full qualified name of the Java Class required. That is a bit more tiring as compared to the first and easier way of importing the package once, and that's all that is required.
So now you must have a good understanding about Java Package. If you like the article then do leave a comment and keep learning Java at Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Sunday, December 21, 2008
What is Java
Welcome back to Java Code Online. Java is one of the most powerful tools available for programming right now. Java was originally created by James Gosling. Originally names as "Oak", it was named as "Java" in 1995. Java was built on the Principle of OOPS, which actually provided it a very robust structure.
Java was built with the main motive of platform independence. That is to develop a language which could be executed on any platform. The thing that makes Java special is the feature that you write your code once, compile it once and then run it anywhere. This has been possible by the use of Bytecode in Java.
When the developer writes code in Java, and then compiles the code, the compiler changes the code not into executable code, but into Bytecode. This Bytecode is special because it requires JVM to run. So now any machine which has JVM installed on it, can run this Java bytecode. The JVM provides a shell like structure, which encapsulates all the processing of the Java bytecode, so nothing goes out of this shell.
This is good because, it helps in maintaining security and portability of the code. Currently any system you can think of like your mobile, refrigerator, microwave oven, etc., have somewhere Java inbuilt in them. That's the power of Java.
The syntax of Java is very much derived from C and C++, this has been done intentionally, so as to make easy for the developers to switch from C or C++ to Java, thereby making the life of the developer easy, and allowing easy adoption of Java in the programming community.
Today Java is everywhere. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Java was built with the main motive of platform independence. That is to develop a language which could be executed on any platform. The thing that makes Java special is the feature that you write your code once, compile it once and then run it anywhere. This has been possible by the use of Bytecode in Java.
When the developer writes code in Java, and then compiles the code, the compiler changes the code not into executable code, but into Bytecode. This Bytecode is special because it requires JVM to run. So now any machine which has JVM installed on it, can run this Java bytecode. The JVM provides a shell like structure, which encapsulates all the processing of the Java bytecode, so nothing goes out of this shell.
This is good because, it helps in maintaining security and portability of the code. Currently any system you can think of like your mobile, refrigerator, microwave oven, etc., have somewhere Java inbuilt in them. That's the power of Java.
The syntax of Java is very much derived from C and C++, this has been done intentionally, so as to make easy for the developers to switch from C or C++ to Java, thereby making the life of the developer easy, and allowing easy adoption of Java in the programming community.
Today Java is everywhere. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Friday, December 19, 2008
How to create a new Object in Java
Welcome back to Java Code Online. Creating of Objects is a very easy thing, if you understand the actual processing going on at the back end. Understanding how to create a new Object is essential for getting a good hold on the Principle of OOPS and Java itself.
For creating a new Object in Java, we use the keyword "new". Simple isn't it. For illustrating this concept, we create a class called BMW, and then make a new Object of this class in a new class called TestDrive. Here we will step by step understand the steps involved in creation of a new Object and its importance on the heap. Oh! I forget to tell you, every Object in Java lives on the heap. Every time you create a new Object it takes some space on the heap, which must be cleared once the Object gets useless or out of reach. For this we use Garbage Collection in Java.
Anyhow, here is your first class.
class BMW
{
String color,
int speed;
void showSpeed()
{
System.out.println("Speed is 100 miles/hr");
}
}
class TestDrive
{
Public Static void main(String args[])
{
BMW b = new BMW();
b.color = "red";
b.showSpeed();
}
}
The above class TestDrive when runs, it prints the output as "Speed is 100 miles/hr". In the above program, we have created a class called BMW and in that defined its instance variables and methods. Then we have defined a seprate class called TestDrive in which we make an Object of type BMW, we set the color of this BMW to red, and then we call the showSpeed() method, which displays the speed of the BMW.
Here the "." operator in b.showSpeed() is used for calling the showSpeed function of the BMW. The main thing here is that we have created a new Object by using the keyword "new".
When we say:-
BMW b = new BMW();
Here there are three steps involved, they are:-
1. Declaration of a reference variable
2. Creation of an Object
3. Assignment of the new Object to the refernce variable.
I hope you got my point. Anyhow I will explain it again. When we say:-
BMW b
we have just created a new refernve variable of type BMW. And then we say:-
new BMW();
here we have just created a new Object of type BMW. And then we equate them by using the "=" operator.
So now, we have created a new Object and then assigned it a reference variable, and we have done this because now we can use this reference variable to perform operations on this Object.
Now I am pretty sure that you have got my point. In case you like my article, do leave me a comment. For more information on Java, keep buzzing Java Code Online.
For creating a new Object in Java, we use the keyword "new". Simple isn't it. For illustrating this concept, we create a class called BMW, and then make a new Object of this class in a new class called TestDrive. Here we will step by step understand the steps involved in creation of a new Object and its importance on the heap. Oh! I forget to tell you, every Object in Java lives on the heap. Every time you create a new Object it takes some space on the heap, which must be cleared once the Object gets useless or out of reach. For this we use Garbage Collection in Java.
Anyhow, here is your first class.
class BMW
{
String color,
int speed;
void showSpeed()
{
System.out.println("Speed is 100 miles/hr");
}
}
class TestDrive
{
Public Static void main(String args[])
{
BMW b = new BMW();
b.color = "red";
b.showSpeed();
}
}
The above class TestDrive when runs, it prints the output as "Speed is 100 miles/hr". In the above program, we have created a class called BMW and in that defined its instance variables and methods. Then we have defined a seprate class called TestDrive in which we make an Object of type BMW, we set the color of this BMW to red, and then we call the showSpeed() method, which displays the speed of the BMW.
Here the "." operator in b.showSpeed() is used for calling the showSpeed function of the BMW. The main thing here is that we have created a new Object by using the keyword "new".
When we say:-
BMW b = new BMW();
Here there are three steps involved, they are:-
1. Declaration of a reference variable
2. Creation of an Object
3. Assignment of the new Object to the refernce variable.
I hope you got my point. Anyhow I will explain it again. When we say:-
BMW b
we have just created a new refernve variable of type BMW. And then we say:-
new BMW();
here we have just created a new Object of type BMW. And then we equate them by using the "=" operator.
So now, we have created a new Object and then assigned it a reference variable, and we have done this because now we can use this reference variable to perform operations on this Object.
Now I am pretty sure that you have got my point. In case you like my article, do leave me a comment. For more information on Java, keep buzzing Java Code Online.
Thursday, December 18, 2008
What is an abstract Class
Welcome back to Java Code Online. This is one of the most commonly asked interview questions. Now that we have learned What is Abstraction and What is a Class, we are ready to learn what is an abstract class in Java. If anyone is not clear about abstraction, then I do insist that first read my previous post of What is Abstraction. This will help you in a better understanding of this concept.
An abstract class in Java is one which cannot be instantiated. I know that this was bouncer for you, but this is the actual definition of abstract class. I will explain you what I mean by this. In my last article on What is Abstraction, I discussed about the Tiger Object and the abstract class called Animal. So now we are aware that the animal class is an abstract class.
We know that we can create new Objects in Java using the "new" operator. So what will happen, when someone write the code as:-
Animal anim = new Animal();
This is the code for instantiating an Animal class, but wait, you can't define an Animal Object. It is too abstract to define it. What color it would be, what size it would be, what shape it would be. There are so many questions for that. Infact the abstract classes should never be instantiated. You can do this by writing the keyword "abstract" before the access modifier of the class. I will show you how:-
abstract public class Animal
{
public void color()
{
// Your code here.
}
}
Now the above class is declared as abstract. So whenever anyone tries to make a new Object of type Animal by saying that:-
Animal anim = new Animal();
The compiler will complain with the error:-
Animal is abstract; cannot be instantiated
Animal anim = new Animal();
1 error
If you don't want your compiler to complain, you better take care of this point.
Abstract classes are practically useless if not extended. By this I mean that, if you cannot create an Object of an abstract class in Java, what good it would be for you. You cannot perform operation on the class, infact you can do nothing on that class. An abstract class is only helpful, if it is extended. Here I am taking about Inheritance, which is another strong Principle of OOPS. Actually all the four Principle of OOPS abstraction, encapsulation, inheritance and polymorphism work together in making Java as a very powerful tool for development of applications.
So I was taking about extending an abstract class. When you extend an Abstract class, you inherit all the methods and instance variable defined in the abstract class, then on that you can perform your operations. I will discuss more on this in my article on Inheritance. For the time being just concentrate on the abstract class part.
So now you are aware that an abstract class should not be instantiated, and you can avoid it by using the keyword "abstract". If you like this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
An abstract class in Java is one which cannot be instantiated. I know that this was bouncer for you, but this is the actual definition of abstract class. I will explain you what I mean by this. In my last article on What is Abstraction, I discussed about the Tiger Object and the abstract class called Animal. So now we are aware that the animal class is an abstract class.
We know that we can create new Objects in Java using the "new" operator. So what will happen, when someone write the code as:-
Animal anim = new Animal();
This is the code for instantiating an Animal class, but wait, you can't define an Animal Object. It is too abstract to define it. What color it would be, what size it would be, what shape it would be. There are so many questions for that. Infact the abstract classes should never be instantiated. You can do this by writing the keyword "abstract" before the access modifier of the class. I will show you how:-
abstract public class Animal
{
public void color()
{
// Your code here.
}
}
Now the above class is declared as abstract. So whenever anyone tries to make a new Object of type Animal by saying that:-
Animal anim = new Animal();
The compiler will complain with the error:-
Animal is abstract; cannot be instantiated
Animal anim = new Animal();
1 error
If you don't want your compiler to complain, you better take care of this point.
Abstract classes are practically useless if not extended. By this I mean that, if you cannot create an Object of an abstract class in Java, what good it would be for you. You cannot perform operation on the class, infact you can do nothing on that class. An abstract class is only helpful, if it is extended. Here I am taking about Inheritance, which is another strong Principle of OOPS. Actually all the four Principle of OOPS abstraction, encapsulation, inheritance and polymorphism work together in making Java as a very powerful tool for development of applications.
So I was taking about extending an abstract class. When you extend an Abstract class, you inherit all the methods and instance variable defined in the abstract class, then on that you can perform your operations. I will discuss more on this in my article on Inheritance. For the time being just concentrate on the abstract class part.
So now you are aware that an abstract class should not be instantiated, and you can avoid it by using the keyword "abstract". If you like this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Wednesday, December 17, 2008
What is a Class
Welcome back to Java Code Online. Class is the most commonly thing in Java. Having a good understanding of class is essential for getting a firm grip on the Principle of OOPS and Java itself. A class is called the blueprint of an Object. Everything that is done in Java, has to be enclosed within a class.
As I said before that the class is a blueprint of an Object, it is used for making Objects. I Java we know that everything is an Object, and if we don't know then we must know this. Java is built on the Principle of OOPS and is totally Object Oriented. So Java see everything as an Object.
I will take you through a through understanding of this concept. A class is used to define a new data type, and normally a separate class is used to make Objects of this new data type. Think of a class as something which defines what an Object knows and what an Object does. Therefore a class is also called a template for an Object and an Object is called an instance of the class. Thus a class contains the instance variables (things which an Object knows) and methods (things which an Object does).
A class is just like a blueprint and it covers every intricate detail about the Object that will be made from it. The normal structure of class is that it defines everything about an Object, so suppose that your application consists of some Objects, and so you make classes for each. Now you define a main class which will initiate all the calling procedure.
The main class always contain the method "main". This method tell the JVM that this is the main class and it need to start execution from here. Once in the main class, you can define Objects of other classes that you have made. I will take a simple example to clarify my point.
// This program declares two Car objects.
class Square {
int width;
int height;
}
class SquareDemo2 {
public static void main(String args[]) {
Square mySquare1 = new Square();
Square mySquare2 = new Square();
int area;
// assign values to mySquare1's instance variables
mySquare1.width = 10;
mySquare1.height = 20;
/* assign different values to mySquare2's instance variables */
mySquare2.width = 15;
mySquare2.height = 15;
// compute area of first Square
area = mySquare1.width * mySquare1.height;
System.out.println("Area is " + area);
// compute area of second box
area = mySquare2.width * mySquare2.height;
System.out.println("Area is " + area);
}
}
The first System.out.println results in:- 200
The second System.out.println results in:- 225
Now you must be wondering about so many new things, I have shown above. I will cover each of them.
Here we have defined two classes Square and SquareDemo2. The first class is used for making an Object of type Car. The second Class uses this new data type for making new Objects. Now as told before, the execution begins from the class containing the "main" method.
Now as you can see that there are two different outputs for the two Objects, this shows that both the Objects are unique and are independent of each other. Here System.out.println is used for displaying the results at the console or output screen.
The lines starting with // are skipped by the Java compiler. Infact the // is the syntax for Comment in Java. These are very helpful in explaining the functioning of the code.
If you have any problems with the code that I have explained right now, then do leave me a comment and I will try my best to clarify your doubt.
The most important thing that you must have noticed here is the parameter "new". This is the magic keyword of Java and is used for creating new Objects. I will discuss more on creating a new Object in my later posts.
I hope the article was helpful in explaining you about What is a Class. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
As I said before that the class is a blueprint of an Object, it is used for making Objects. I Java we know that everything is an Object, and if we don't know then we must know this. Java is built on the Principle of OOPS and is totally Object Oriented. So Java see everything as an Object.
I will take you through a through understanding of this concept. A class is used to define a new data type, and normally a separate class is used to make Objects of this new data type. Think of a class as something which defines what an Object knows and what an Object does. Therefore a class is also called a template for an Object and an Object is called an instance of the class. Thus a class contains the instance variables (things which an Object knows) and methods (things which an Object does).
A class is just like a blueprint and it covers every intricate detail about the Object that will be made from it. The normal structure of class is that it defines everything about an Object, so suppose that your application consists of some Objects, and so you make classes for each. Now you define a main class which will initiate all the calling procedure.
The main class always contain the method "main". This method tell the JVM that this is the main class and it need to start execution from here. Once in the main class, you can define Objects of other classes that you have made. I will take a simple example to clarify my point.
// This program declares two Car objects.
class Square {
int width;
int height;
}
class SquareDemo2 {
public static void main(String args[]) {
Square mySquare1 = new Square();
Square mySquare2 = new Square();
int area;
// assign values to mySquare1's instance variables
mySquare1.width = 10;
mySquare1.height = 20;
/* assign different values to mySquare2's instance variables */
mySquare2.width = 15;
mySquare2.height = 15;
// compute area of first Square
area = mySquare1.width * mySquare1.height;
System.out.println("Area is " + area);
// compute area of second box
area = mySquare2.width * mySquare2.height;
System.out.println("Area is " + area);
}
}
The first System.out.println results in:- 200
The second System.out.println results in:- 225
Now you must be wondering about so many new things, I have shown above. I will cover each of them.
Here we have defined two classes Square and SquareDemo2. The first class is used for making an Object of type Car. The second Class uses this new data type for making new Objects. Now as told before, the execution begins from the class containing the "main" method.
Now as you can see that there are two different outputs for the two Objects, this shows that both the Objects are unique and are independent of each other. Here System.out.println is used for displaying the results at the console or output screen.
The lines starting with // are skipped by the Java compiler. Infact the // is the syntax for Comment in Java. These are very helpful in explaining the functioning of the code.
If you have any problems with the code that I have explained right now, then do leave me a comment and I will try my best to clarify your doubt.
The most important thing that you must have noticed here is the parameter "new". This is the magic keyword of Java and is used for creating new Objects. I will discuss more on creating a new Object in my later posts.
I hope the article was helpful in explaining you about What is a Class. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Tuesday, December 16, 2008
Comments in Java
Welcome back to Java Code Online. This is a small article on the type of comments available in Java. A comment is line in the Java Program which is not executed by the JVM. The syntax of the comment tells the JVM, that this line or these lines are not to be compiled and these lines of code need to be skipped.
Comments help in Java code by making the code clearer, providing a mechanism to maintain a history in the java files, and helps the developer in understanding what the code is doing. History is helpful in telling the developer that what is this class for, and what was the last time it was updated. Comments are an essential component of Java, and must be taken into practice, if you want to make your and others life easy.
Suppose that you write a code for making a dog Object. After around a year or so, there is a change requirement or some bug found in the code. Now you come back to the same code, but you can't remember what was the functionality of the code that you have written one year back. In this case comments help the developer remember the functioning of the code.
Java has two types of comments possible in its code. They are:-
1. Single line comment
2. Multiple line comment
Java provides you the option to comment a single line at a time, or to comment multiple lines at the same time.
For commenting a single line, use double forward slash(//) before the line you want to comment. For example:-
// The code below is for making a new Dog Object
Dog d = new Dog();
In the two lines of code written above, the first line will be skipped by the JVM, and will jump to the second line. The first line helps to tell the developer that the code below is for making a new Dog Object.
There are instances when you would like to comment multiple lines at one shot. Suppose that your comments span to multiple lines, then will you proceed by putting a "//" before every line. It would be tedious, so Java provides a different mechanism for this , the syntax for commenting multiple lines of Java is:-
/* The code is below for making a Dog Object
This code has the benefit that it simplifies the logic.
This is a multiple line comment.
And you are reading its syntax */
Dog d = new Dog();
Here everything in between "/*" and "*/" are skipped by the JVM compiler. So you can now comment multiple lines of code easily. If this article was beneficial then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Comments help in Java code by making the code clearer, providing a mechanism to maintain a history in the java files, and helps the developer in understanding what the code is doing. History is helpful in telling the developer that what is this class for, and what was the last time it was updated. Comments are an essential component of Java, and must be taken into practice, if you want to make your and others life easy.
Suppose that you write a code for making a dog Object. After around a year or so, there is a change requirement or some bug found in the code. Now you come back to the same code, but you can't remember what was the functionality of the code that you have written one year back. In this case comments help the developer remember the functioning of the code.
Java has two types of comments possible in its code. They are:-
1. Single line comment
2. Multiple line comment
Java provides you the option to comment a single line at a time, or to comment multiple lines at the same time.
For commenting a single line, use double forward slash(//) before the line you want to comment. For example:-
// The code below is for making a new Dog Object
Dog d = new Dog();
In the two lines of code written above, the first line will be skipped by the JVM, and will jump to the second line. The first line helps to tell the developer that the code below is for making a new Dog Object.
There are instances when you would like to comment multiple lines at one shot. Suppose that your comments span to multiple lines, then will you proceed by putting a "//" before every line. It would be tedious, so Java provides a different mechanism for this , the syntax for commenting multiple lines of Java is:-
/* The code is below for making a Dog Object
This code has the benefit that it simplifies the logic.
This is a multiple line comment.
And you are reading its syntax */
Dog d = new Dog();
Here everything in between "/*" and "*/" are skipped by the JVM compiler. So you can now comment multiple lines of code easily. If this article was beneficial then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Monday, December 15, 2008
What is Bytecode in Java
Welcome back to Java Code Online. ByteCode is the biggest improvement in Java, when compared to other languages like C++. I will discuss what is Bytecode and why it is needed, and what is the benefit of using Bytecode in Java.
Most of the languages that developers work in face two main problems. They are:-
1. Security
2. Portability
Security
We all know that, while downloading any file from anywhere on the internet, we face two major problems, they are a viral infection, that is the file may be corrupted with virus. And the second is that the file may contain a spyware, that is some malicious code in the file scans your computer for your data. No one likes that, but that's a big reality.
Portability
We all want that, once we do the coding, we can easily take the output to many other platforms. But the issue is that most of the systems, don't provide compatibility to the previous code. It may be that you are coding on the windows machine and tomorrow you want to run the code on a linux machine, in that case there could be portability issues. Java has the benefit of "code once and use anywhere". I will tell you how.
Java faces both of these threats by a simple means called the Bytecode. Bytecode is a highly optimized code that runs only on JVM(Java Virtual Machine). The issue of virus and spyware protection is solved by making sure that, everything runs with the JVM environment, and nothing goes out of that, so there is no chance of getting the files infected by virus or spyware. JVM provides a complete shell like environment which encloses all the processing of the Java operations.
The issue of portability is again resolved by using Bytecode. Bytecode is not the normal executable code, it need the JVM for its operations. So you just need to ensure that JVM is installed on the system on which you are working, and the Bytecode will run perfectly fine. So infact you may code the program once, compile it to convert it into Bytecode, and then use the Bytecode on any JVM compatible machine. So it is simply "Code once and use anywhere". That's the power of Java.
The Bytecode is a highly optimized code which is understood only by the JVM. The JVM is the interpreter of the ByteCode. There could be various versions of JVM operating on various machines, but all interpret the same Bytecode, and so it provides a highly cool feature of portability.
I hope that the article was beneficial in explaining you, what is Bytecode in Java, and what are it's advantages. If you liked the article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Most of the languages that developers work in face two main problems. They are:-
1. Security
2. Portability
Security
We all know that, while downloading any file from anywhere on the internet, we face two major problems, they are a viral infection, that is the file may be corrupted with virus. And the second is that the file may contain a spyware, that is some malicious code in the file scans your computer for your data. No one likes that, but that's a big reality.
Portability
We all want that, once we do the coding, we can easily take the output to many other platforms. But the issue is that most of the systems, don't provide compatibility to the previous code. It may be that you are coding on the windows machine and tomorrow you want to run the code on a linux machine, in that case there could be portability issues. Java has the benefit of "code once and use anywhere". I will tell you how.
Java faces both of these threats by a simple means called the Bytecode. Bytecode is a highly optimized code that runs only on JVM(Java Virtual Machine). The issue of virus and spyware protection is solved by making sure that, everything runs with the JVM environment, and nothing goes out of that, so there is no chance of getting the files infected by virus or spyware. JVM provides a complete shell like environment which encloses all the processing of the Java operations.
The issue of portability is again resolved by using Bytecode. Bytecode is not the normal executable code, it need the JVM for its operations. So you just need to ensure that JVM is installed on the system on which you are working, and the Bytecode will run perfectly fine. So infact you may code the program once, compile it to convert it into Bytecode, and then use the Bytecode on any JVM compatible machine. So it is simply "Code once and use anywhere". That's the power of Java.
The Bytecode is a highly optimized code which is understood only by the JVM. The JVM is the interpreter of the ByteCode. There could be various versions of JVM operating on various machines, but all interpret the same Bytecode, and so it provides a highly cool feature of portability.
I hope that the article was beneficial in explaining you, what is Bytecode in Java, and what are it's advantages. If you liked the article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Sunday, December 14, 2008
What is Abstraction
Welcome back to Java Code Online. Abstraction is the first Principle of OOPS and one of the core concepts of Java. First of all if you really want to get a hold on the Principle of OOPS and gain a strong understanding on the concepts of Java, then forget everything you have read about Abstraction and start from scratch. Trust me, you won't be disappointed.
Abstraction in laymen term means taking out the common things. First of all concentrate on the word Abstraction, it comes from the word "Abstract". Here you abstract the most common features of an Object and place them in a separate class. I will take some real life examples to make your life more easy.
Suppose that you need to make an Object of Tiger type. I mean to say that, you need a class for making a Tiger. Now just think that, what does a Tiger "do", and what does a Tiger "know". The stress her is on the words "do" and "know", because whatever the Tiger "do" are its methods and whatever the Tiger "knows" are its instance variables.
Now we know that every Tiger has a some basic properties like hunger, location, color, etc. These are the instance variables of Tiger. And every Tiger "does" some basic things like makes noise, eat, sleep, roam around, etc.
Now if we have to make only Tiger Objects then a class for Tiger is ok. But what if we need to add other animals too, like cat, dog, wolf etc. Will you write a seprate class for every animal, knowing that every animal has the same basic properties. Won't it be a waste of energy and time, rewriting the same methods again for every animal.
Here is where Abstraction comes into picture. In abstraction, what you do is take out the common instance variables and methods, so that you need not write them again, you write the basic methods and instance variables in a single abstract class, and then you "extend" other classes from it depending upon their behavior. And just by magic all the methods and instance variables in the abstract class are available to the "extended" classes. The part of extending a class is called Inheritance and is the second Principle of OOPS, I will discuss it in my later posts. For the time being just concentrate on the word Abstraction.
So now for the Tiger class, we make a parent abstract class called Animal. We know that all the animals have more or less the same features. So we write the instance variables like color, hunger, location, etc. into the abstract class. And also we define the methods common to all the animals like makeNoise(), eat(),
roam(), sleep(), etc. and then use these methods and instance variables in the child classes. So now even a wolf, cat and dog can use the code of the abstract animal class, and no need to rewrite the code again for them.
So in technical terms Abstraction is taking out the common code from similar classes and placing it in one particular class, so that it could be used by all the classes using the same functionality. It helps in code reusability, makes the code clearer and better to read and interpret, and most of all makes your life easy.
I hope that I was able to keep my word in explaining you about what is Abstraction. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Related Articles
Principle of OOPS
Advantage of Abstraction
What is Object
Abstraction in laymen term means taking out the common things. First of all concentrate on the word Abstraction, it comes from the word "Abstract". Here you abstract the most common features of an Object and place them in a separate class. I will take some real life examples to make your life more easy.
Suppose that you need to make an Object of Tiger type. I mean to say that, you need a class for making a Tiger. Now just think that, what does a Tiger "do", and what does a Tiger "know". The stress her is on the words "do" and "know", because whatever the Tiger "do" are its methods and whatever the Tiger "knows" are its instance variables.
Now we know that every Tiger has a some basic properties like hunger, location, color, etc. These are the instance variables of Tiger. And every Tiger "does" some basic things like makes noise, eat, sleep, roam around, etc.
Now if we have to make only Tiger Objects then a class for Tiger is ok. But what if we need to add other animals too, like cat, dog, wolf etc. Will you write a seprate class for every animal, knowing that every animal has the same basic properties. Won't it be a waste of energy and time, rewriting the same methods again for every animal.
Here is where Abstraction comes into picture. In abstraction, what you do is take out the common instance variables and methods, so that you need not write them again, you write the basic methods and instance variables in a single abstract class, and then you "extend" other classes from it depending upon their behavior. And just by magic all the methods and instance variables in the abstract class are available to the "extended" classes. The part of extending a class is called Inheritance and is the second Principle of OOPS, I will discuss it in my later posts. For the time being just concentrate on the word Abstraction.
So now for the Tiger class, we make a parent abstract class called Animal. We know that all the animals have more or less the same features. So we write the instance variables like color, hunger, location, etc. into the abstract class. And also we define the methods common to all the animals like makeNoise(), eat(),
roam(), sleep(), etc. and then use these methods and instance variables in the child classes. So now even a wolf, cat and dog can use the code of the abstract animal class, and no need to rewrite the code again for them.
So in technical terms Abstraction is taking out the common code from similar classes and placing it in one particular class, so that it could be used by all the classes using the same functionality. It helps in code reusability, makes the code clearer and better to read and interpret, and most of all makes your life easy.
I hope that I was able to keep my word in explaining you about what is Abstraction. If you liked the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Related Articles
Principle of OOPS
Advantage of Abstraction
What is Object
Saturday, December 13, 2008
What is an identifier in Java
Welcome back to Java Code Online. Java is a very powerful language. The power in it comes from the Principle of OOPS that it follows. Identifiers in Java are used for naming a variable, method, class or label. I will cover each of these topics in detail, in my later posts. For the time being just concentrate on the word identifier.
Identifiers are required by Java, so that you may give your variable a name. Though as seen above it is also used for naming method, class and label. There are some rules that need to be followed, while naming an identifier.
The rules for naming an identifier are simple and must be practiced thoroughly, if you don't want your JVM to shout that it cant compile the class. The rules for naming an identifier are:-
1. An identifier in Java can start with a letter.
2. An identifier in Java can start with a dollar sign($)
3. An identifier in Java can start with an underscore(_)
4. Subsequent characters in the identifier after the first character can be letters, dollar sign, underscore, or a digit.
Any other rule for identifiers is not there, so just stick to these basic four rules and you won't face any problem in compilation of your code.
I will take a few examples so as to make the situation more clear to you.
companyName //legal
BigClass //legal: you may embed the keywords in between
$money //legal
8_node //illegal(Cannot start with a digit)
!node //illegal(special characters not allowed in starting except "$" and "_")
Now you would be able to clearly distinguish, which one is a valid identifier and which one is not. Having good understating of these basic concepts of Java help make the life of the developer easy, as he faces less errors. The life of a developer is already very hard, faced with coding requirements, meeting deadline, and debugging bugs.
So it would be good to avoid small pitfalls like naming an identifier. I hope the article was handy for all those looking for information on Identifiers in Java. If you like the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Identifiers are required by Java, so that you may give your variable a name. Though as seen above it is also used for naming method, class and label. There are some rules that need to be followed, while naming an identifier.
The rules for naming an identifier are simple and must be practiced thoroughly, if you don't want your JVM to shout that it cant compile the class. The rules for naming an identifier are:-
1. An identifier in Java can start with a letter.
2. An identifier in Java can start with a dollar sign($)
3. An identifier in Java can start with an underscore(_)
4. Subsequent characters in the identifier after the first character can be letters, dollar sign, underscore, or a digit.
Any other rule for identifiers is not there, so just stick to these basic four rules and you won't face any problem in compilation of your code.
I will take a few examples so as to make the situation more clear to you.
companyName //legal
BigClass //legal: you may embed the keywords in between
$money //legal
8_node //illegal(Cannot start with a digit)
!node //illegal(special characters not allowed in starting except "$" and "_")
Now you would be able to clearly distinguish, which one is a valid identifier and which one is not. Having good understating of these basic concepts of Java help make the life of the developer easy, as he faces less errors. The life of a developer is already very hard, faced with coding requirements, meeting deadline, and debugging bugs.
So it would be good to avoid small pitfalls like naming an identifier. I hope the article was handy for all those looking for information on Identifiers in Java. If you like the article then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Friday, December 12, 2008
What is the purpose of garbage collection in Java
Welcome back to Java Code OnlineJava Code Online. The garbage collector is one of the best things in Java. In Java every Object lives on a heap. Heap is the play area of Objects. Whenever we create Objects, some area on the heap is allocated to this new Object. But what will happen, when this Object is of no use.
I mean to say that, when the Object refers nothing, then it consumes unnecessary space on the heap, which must be freed. This space could be used by other Objects, and also helps the JVM in speeding up the operations of processing.
This is where the garbage collector comes into picture. The Garbage collector in Java, checks at regular intervals about the Objects that are no more referenced by any variable. These Objects are eligible for Garbage collection. Garbage Collector checks for the Objects that are unreachable for the program, and so are consuming unnecessary space, so Garbage Collector considers these Objects as Garbage, and so it collects them. Thereby freeing space on the heap.
For example, if I say that:-
Object a = new Object();
This line of code allots space on the heap for the new Object. But suppose that later in the code, we say that:-
a = null;
This statement will cause the reference of this Object to refer null, and so the Object is lost in the heap and is now unreachable. This Object is now eligible for Garbage collection, so that space could be freed on the heap. This space helps the JVM in performing better and faster.
I hope that this article was helpful in explaining you the role of Garbage Collector in Java. If you liked this article, then do leave a comment. For more information on Java, keep buzzing Java Code Online.
I mean to say that, when the Object refers nothing, then it consumes unnecessary space on the heap, which must be freed. This space could be used by other Objects, and also helps the JVM in speeding up the operations of processing.
This is where the garbage collector comes into picture. The Garbage collector in Java, checks at regular intervals about the Objects that are no more referenced by any variable. These Objects are eligible for Garbage collection. Garbage Collector checks for the Objects that are unreachable for the program, and so are consuming unnecessary space, so Garbage Collector considers these Objects as Garbage, and so it collects them. Thereby freeing space on the heap.
For example, if I say that:-
Object a = new Object();
This line of code allots space on the heap for the new Object. But suppose that later in the code, we say that:-
a = null;
This statement will cause the reference of this Object to refer null, and so the Object is lost in the heap and is now unreachable. This Object is now eligible for Garbage collection, so that space could be freed on the heap. This space helps the JVM in performing better and faster.
I hope that this article was helpful in explaining you the role of Garbage Collector in Java. If you liked this article, then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Thursday, December 11, 2008
Java Keywords and Reserved Words
Welcome back to Java Code Online. Java keywords are those which are defined by the programming language. These keywords are used by programmers to code easily. You cannot use keywords in place of variable names, that is not allowed.
Reserved words in Java are actually words which are reserved by the programming language. It might be, that the meaning of these keywords is not defined by the compiler but they are reserved. So you cannot use these also for your variable names in Java.
A list of Java Keywords and Reserved Words is given below. This list is correct as per the Tiger version of Java, that is Java 2.
List of Keywords and Reserved Words in Java:-
1. abstract
2. class
3. extends
4. implements
5. null
6. strictfp
7. true
8. assert
9. const
10. false
11. import
12. package
13. super
14. try
15. boolean
16. continue
17. final
18. instanceof
19. private
20. switch
21. void
22. break
23. default
24. finally
25. int
26. protected
27. synchronized
28. volatile
29. byte
30. do
31. float
32. interface
33. public
34. this
35. while
36. case
37. double
38. for
39. long
40. return
50. throw
51. catch
52. else
53. goto
54. native
55. short
56. throws
57. char
58. enum
59. if
60. new
61. static
62. transient
So now you have got a good idea about the various keywords in Java, so remember to not to use these keywords and reserved words in your code, or be ready for errors. I will discuss most of these Java Keywords in later posts.
I hope that this article on Java Keywords and Reserved words was beneficial to you in providing you enough idea about the keywords used in Java. For more information on Java keep buzzing Java Code Online.
Reserved words in Java are actually words which are reserved by the programming language. It might be, that the meaning of these keywords is not defined by the compiler but they are reserved. So you cannot use these also for your variable names in Java.
A list of Java Keywords and Reserved Words is given below. This list is correct as per the Tiger version of Java, that is Java 2.
List of Keywords and Reserved Words in Java:-
1. abstract
2. class
3. extends
4. implements
5. null
6. strictfp
7. true
8. assert
9. const
10. false
11. import
12. package
13. super
14. try
15. boolean
16. continue
17. final
18. instanceof
19. private
20. switch
21. void
22. break
23. default
24. finally
25. int
26. protected
27. synchronized
28. volatile
29. byte
30. do
31. float
32. interface
33. public
34. this
35. while
36. case
37. double
38. for
39. long
40. return
50. throw
51. catch
52. else
53. goto
54. native
55. short
56. throws
57. char
58. enum
59. if
60. new
61. static
62. transient
So now you have got a good idea about the various keywords in Java, so remember to not to use these keywords and reserved words in your code, or be ready for errors. I will discuss most of these Java Keywords in later posts.
I hope that this article on Java Keywords and Reserved words was beneficial to you in providing you enough idea about the keywords used in Java. For more information on Java keep buzzing Java Code Online.
Wednesday, December 10, 2008
What is an Object
Welcome back to Java Code Online. Many of my friends and readers often ask me that "What is an Object?". Understanding the basics of Object is compulsory for getting a hold on the Principle of OOPS and Java itself.
Java is totally built on the fundamental of Object Oriented Programming(OOPS), and OOPS depend upon the understanding about the "Object". So getting a hold on this question is important.
I will start with real life examples, everything that you can see in this world is an Object. Just look around yourself, your bike, table, chair, television, even you yourself and me too are Objects. Every entity is an Object. Now Objects have two basic properties. They are:
1. State
2. Behavior
Whenever you need to access and understand an Object, just ask yourself what are the possible state of this Object and what could be the possible behavior of this Object. For example a table lamp can have two states on and off, while there could be two possible behavior of the table lamp, turn on and turn off.
Similarly for a cat the possible states could be hungry, color, breed, etc. While there could be many states like wagging tail, chasing mouse, etc.
The state of an Object defines what the Object "knows", and the behaviour of the Object defines what the Object "does". In Java a class is used for making an Object. Whatever the Object knows are its instance variables and whatever the Object does are its methods.
The methods In Java operate on the Objects state and provides a mechanism to the outer world to access and operate on them. If you are confused between a class and an Object, then read my previous post on Difference between an Object and a Class.
Keep in mind that an Object is the core of Java, so make sure to read this article properly so as to get a proper hold on "What is an Object?".
I hope that this article was beneficial in explaning you about What is an Object. If you liked this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Java is totally built on the fundamental of Object Oriented Programming(OOPS), and OOPS depend upon the understanding about the "Object". So getting a hold on this question is important.
I will start with real life examples, everything that you can see in this world is an Object. Just look around yourself, your bike, table, chair, television, even you yourself and me too are Objects. Every entity is an Object. Now Objects have two basic properties. They are:
1. State
2. Behavior
Whenever you need to access and understand an Object, just ask yourself what are the possible state of this Object and what could be the possible behavior of this Object. For example a table lamp can have two states on and off, while there could be two possible behavior of the table lamp, turn on and turn off.
Similarly for a cat the possible states could be hungry, color, breed, etc. While there could be many states like wagging tail, chasing mouse, etc.
The state of an Object defines what the Object "knows", and the behaviour of the Object defines what the Object "does". In Java a class is used for making an Object. Whatever the Object knows are its instance variables and whatever the Object does are its methods.
The methods In Java operate on the Objects state and provides a mechanism to the outer world to access and operate on them. If you are confused between a class and an Object, then read my previous post on Difference between an Object and a Class.
Keep in mind that an Object is the core of Java, so make sure to read this article properly so as to get a proper hold on "What is an Object?".
I hope that this article was beneficial in explaning you about What is an Object. If you liked this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.
Tuesday, December 9, 2008
What is the difference between an Object and a Class in Java?
Welcome back to java code online. This is one of the basic question of Java. In Java everything is an Object, but a Class is used to make it. The exact definition is that "A class is a blueprint for an object". Particularly it tells the JVM about how to make the Object.
When I say that a class is a blueprint of an Object, I mean that every Object in Java made from that class is practically the same but can have its own specific values. Like whenever a building is made, then a blueprint is made beforehand. The blueprint defines the structure of the building, but using the same blueprint you may create many separate buildings. Each building can have its own color on the walls, its own particular size, etc. But the overall structure will be the same.
I will take one more example to clarify my point. We know that the process of making a car is totally automated. Now the cavity which molds the shape of the car is the same, but the same cavity is used to mold several cars. Like in the Mercedes factory, each Mercedes coming out from the car molding cavity will be practically the same. Though each Mercedes can later be given its particular color, alloy wheels, extra bumper, advanced music system, etc. Here the cavity which molds the car is the "class" in Java, and the Mercedes car coming out from that cavity is the "Object" in Java.
Now you would be easily able to visualize the difference between a class and an Object in Java. I hope that this article was beneficial in helping you distinguish between an Object and a Class. If you liked the article then do leave a comment. I would love to hear from you. For more info on Java, keep buzzing Java Code Online.
When I say that a class is a blueprint of an Object, I mean that every Object in Java made from that class is practically the same but can have its own specific values. Like whenever a building is made, then a blueprint is made beforehand. The blueprint defines the structure of the building, but using the same blueprint you may create many separate buildings. Each building can have its own color on the walls, its own particular size, etc. But the overall structure will be the same.
I will take one more example to clarify my point. We know that the process of making a car is totally automated. Now the cavity which molds the shape of the car is the same, but the same cavity is used to mold several cars. Like in the Mercedes factory, each Mercedes coming out from the car molding cavity will be practically the same. Though each Mercedes can later be given its particular color, alloy wheels, extra bumper, advanced music system, etc. Here the cavity which molds the car is the "class" in Java, and the Mercedes car coming out from that cavity is the "Object" in Java.
Now you would be easily able to visualize the difference between a class and an Object in Java. I hope that this article was beneficial in helping you distinguish between an Object and a Class. If you liked the article then do leave a comment. I would love to hear from you. For more info on Java, keep buzzing Java Code Online.
Labels:
Java Basics,
Java Interview Questions,
Java Tutorial
Monday, December 8, 2008
Principle of OOPS
Welcome back to Java Code Online.
Object Oriented Programming(OOPS) is the basic heart of JAVA. Java is built on the principle of OOPS. The name OOPS explains its meaning. It implies that in Java everything is an Object. You need to think of it in the same lines.
For the persons who are new to Java technology, face some problem in visualizing things as an Object. For making it more clear, I will start from the scratch.
There are basically two approaches to programming. They are:-
1. Process Oriented
2. Object Oriented
Most of the languages are Process Oriented, the most common example of this is the language C. I will explain both of them now.
Process Oriented
A Process Oriented language is one which stress on the process. The exact word for the process here will be behavior. Like whatever an Object does is its behavior. I will explain it better with the help of an example later in this article.
Object Oriented
An Object Oriented language thinks of everything as an Object or as an entity in itself. The best part is that it makes the entity important then its behavior. Like if a person drives a car, then he thinks of the car as an Object and is not concerned about its behavior like the braking system, the steering, the automatic ignition, etc. All these process or the behavior of the car are taken car by itself. The person here is just concerned about the Object car.
I will take an example that will clear the situation further. Suppose that a developer is given the requirement that he has to implement three shapes one square, one rectangle and one triangle. Each of these when clicked, rotates and plays a sound. Then the two approaches will proceed as follows.
Process Oriented approach stress on the behavior that is rotate and play sound. So in this approach the developer will create the procedures rotate(shape) and play(shape) and he is done. Whatever be the shape just pass it to the procedure and it will make it rotate or play the sound.
Object Oriented approach will proceed with the Objects concerned. Here the developer will see that he has got three separate Objects, a square, a rectangle and a triangle. He will make separate classes for each of them and write the methods rotate() and play(), and he is done. Anyone wishing to call a particular Object can directly call the particular class.
At first glance it seems that Process Oriented is better then Object Oriented, but it is the other way. I will tell you how. The Process Oriented approach is fixed, what will happen when there is a requirement for a new shape that rotates and play sound in a different manner then the previous ones, you never know when the requirements change in this world. In this case the developer will be stuck, while the developer following Object Oriented approach will just make a new class for the new shape and write the code for its rotation in the particular way and play sound in the particular way.
So the main thing that is clear is that with Object Oriented approach, more flexibility is achieved. Now regarding making a new class each time for an Object is a bit tedious, but OOPS has a special way to simplify it. It is called abstraction.
In fact OOPS is like platform which is supported by four pillars. And the pillars are:-
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
I will discuss each of these pillars in the later posts. If you like the article then please do leave a comment, I will love to hear from you. For more information on Java keep buzzing Java Code Online.
Related Articles
What is Abstraction
Advantage of Abstraction
What is Object
Object Oriented Programming(OOPS) is the basic heart of JAVA. Java is built on the principle of OOPS. The name OOPS explains its meaning. It implies that in Java everything is an Object. You need to think of it in the same lines.
For the persons who are new to Java technology, face some problem in visualizing things as an Object. For making it more clear, I will start from the scratch.
There are basically two approaches to programming. They are:-
1. Process Oriented
2. Object Oriented
Most of the languages are Process Oriented, the most common example of this is the language C. I will explain both of them now.
Process Oriented
A Process Oriented language is one which stress on the process. The exact word for the process here will be behavior. Like whatever an Object does is its behavior. I will explain it better with the help of an example later in this article.
Object Oriented
An Object Oriented language thinks of everything as an Object or as an entity in itself. The best part is that it makes the entity important then its behavior. Like if a person drives a car, then he thinks of the car as an Object and is not concerned about its behavior like the braking system, the steering, the automatic ignition, etc. All these process or the behavior of the car are taken car by itself. The person here is just concerned about the Object car.
I will take an example that will clear the situation further. Suppose that a developer is given the requirement that he has to implement three shapes one square, one rectangle and one triangle. Each of these when clicked, rotates and plays a sound. Then the two approaches will proceed as follows.
Process Oriented approach stress on the behavior that is rotate and play sound. So in this approach the developer will create the procedures rotate(shape) and play(shape) and he is done. Whatever be the shape just pass it to the procedure and it will make it rotate or play the sound.
Object Oriented approach will proceed with the Objects concerned. Here the developer will see that he has got three separate Objects, a square, a rectangle and a triangle. He will make separate classes for each of them and write the methods rotate() and play(), and he is done. Anyone wishing to call a particular Object can directly call the particular class.
At first glance it seems that Process Oriented is better then Object Oriented, but it is the other way. I will tell you how. The Process Oriented approach is fixed, what will happen when there is a requirement for a new shape that rotates and play sound in a different manner then the previous ones, you never know when the requirements change in this world. In this case the developer will be stuck, while the developer following Object Oriented approach will just make a new class for the new shape and write the code for its rotation in the particular way and play sound in the particular way.
So the main thing that is clear is that with Object Oriented approach, more flexibility is achieved. Now regarding making a new class each time for an Object is a bit tedious, but OOPS has a special way to simplify it. It is called abstraction.
In fact OOPS is like platform which is supported by four pillars. And the pillars are:-
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
I will discuss each of these pillars in the later posts. If you like the article then please do leave a comment, I will love to hear from you. For more information on Java keep buzzing Java Code Online.
Related Articles
What is Abstraction
Advantage of Abstraction
What is Object
What is the reason for each keyword of public static void main(String args[])?
Hi, Welcome back to java code online. Most of my friends and readers have asked me this basic question of Java, that what is the reason and use of each of the keywords in "public static void main(String args[])". So here we go.
The reason for each of these keywords as used in Java is explained below.
public: "public" implies the accessibility criteria, since it is used with the "main" method of the code, it has to be public. That is it can be accessed from anywhere. I will talk about the other accessibility modifiers later. For the time being, just bear in mind that if the method is public, then it can be accessed from anywhere.
static: Much can be written about this keyword. For the time being, just bear in mind that, Java environment should be able to call this method without creating an instance of the class, that is the reason for declaring it as "static". Static means that no instance of the class be created for accessing it.
void: "void" is a common keyword and is the same as in C. It implies the return type of the method, since the main does not return anything so the return type must be void.
main: "main" is the first method called by java environment whenever a program is executed. Hence the access specifier has to be public. Moreover it tells the JVM that it is the "main" method of the code, so it needs to start from this method.
String: "String" indicates that the method takes parameters from the command line in form of String. It implies the type of the values returned from the Command Line.
args[]: "args[]" implies that the value or arguments entered from the Command Line are an array.
So overall it implies that public static void main(String args[]) is the main method of the class from which the JVM needs to start, and it is public so that it could be accesed from anywhere in the application. This method is static, and so the class need not be instantiated for using it, and it takes an array of arguments from the Command Line which are of String type.
I hope the information was helpful to you. If you like the solution, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
The reason for each of these keywords as used in Java is explained below.
public: "public" implies the accessibility criteria, since it is used with the "main" method of the code, it has to be public. That is it can be accessed from anywhere. I will talk about the other accessibility modifiers later. For the time being, just bear in mind that if the method is public, then it can be accessed from anywhere.
static: Much can be written about this keyword. For the time being, just bear in mind that, Java environment should be able to call this method without creating an instance of the class, that is the reason for declaring it as "static". Static means that no instance of the class be created for accessing it.
void: "void" is a common keyword and is the same as in C. It implies the return type of the method, since the main does not return anything so the return type must be void.
main: "main" is the first method called by java environment whenever a program is executed. Hence the access specifier has to be public. Moreover it tells the JVM that it is the "main" method of the code, so it needs to start from this method.
String: "String" indicates that the method takes parameters from the command line in form of String. It implies the type of the values returned from the Command Line.
args[]: "args[]" implies that the value or arguments entered from the Command Line are an array.
So overall it implies that public static void main(String args[]) is the main method of the class from which the JVM needs to start, and it is public so that it could be accesed from anywhere in the application. This method is static, and so the class need not be instantiated for using it, and it takes an array of arguments from the Command Line which are of String type.
I hope the information was helpful to you. If you like the solution, then do leave a comment. For more info on Java, keep buzzing Java Code Online.
Sunday, December 7, 2008
Welcome to Java Code Online
Welcome to Java Code Online. This site provides tips and techniques for Java Coding and Examples. Here I will discuss Java Fundamentals, Java Code, commonly faced Java errors,etc.
Here I will also discuss some other technologies that are used for developing web applications which include Java, SQL, JavaScript, HTML, JSP, XML,etc. I will try and discuss some of the common Java Projects, that could be developed by freshers for their study and project related requirements.
I hope that this site on Java, will benefit many. If you have questions not covered in Java Code Online please feel free to email your question to amitberiwal@gmail.com
Here I will also discuss some other technologies that are used for developing web applications which include Java, SQL, JavaScript, HTML, JSP, XML,etc. I will try and discuss some of the common Java Projects, that could be developed by freshers for their study and project related requirements.
I hope that this site on Java, will benefit many. If you have questions not covered in Java Code Online please feel free to email your question to amitberiwal@gmail.com
Subscribe to:
Posts (Atom)