While we believe a Java program, it can be described as a compilation of objects that converse via invoking every other's techniques. Let us currently briefly see what class, object, techniques, and example variables signify.
Let us seem at an easy code that will print the utterances Hello World.
public class MyFirstJavaProgram { /* This is my first java program. * This will print 'Hello World' as the output */ public static void main(String []args) { System.out.println("Hello World"); // prints Hello World } }
Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −
C:\> javac MyFirstJavaProgram.java C:\> java MyFirstJavaProgram Hello World
About Java programs, it is extremely significant to remember the following points.
When saving the file, you should save it by the class name (Remember Java is case sensitive) and append '.java' to the end of the name.
However, please create a note that lest you do not possess a public class here in the file then the file name can be diverse than the class name. It is as well not compulsory to have a public class in the file.
Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'
All Java constituents necessitate names. Names utilized for classes, variables, and methods are recognized identifiers.
Like other programming languages, it is likely to adapt classes, methods, etc., with modifiers. There are two categories of modifiers −
We will be searching into more features about modifiers in the next section.
Following are the sorts of variables in Java
Selections are objects that lay up multiple variables of the equivalent type. However, an arrangement itself is an object on the bundle. We will appear into how to announce, construct, and initialize in the future chapters.
Enums were set up in Java 5.0. Enums limit a variable to contain one of only a few predefined values. The values in this detailed list are called enums.
With the apply of enums, it is the potential to lessen the number of bugs in your code.
For instance, if we think of an application for a new juice shop, it would be likely to limit the glass size to little, medium, and large. This would confirm that it would not let anyone individually any size other than a minute, medium, or large.
class FreshJuice { enum FreshJuiceSize{ SMALL, MEDIUM, LARGE } FreshJuiceSize size; } public class FreshJuiceTest { public static void main(String args[]) { FreshJuice juice = new FreshJuice(); juice.size = FreshJuice.FreshJuiceSize.MEDIUM ; System.out.println("Size: " + juice.size); } }
The above example will produce the following result:
Size: MEDIUM
Note − Enums can be stated as possessors within a class. Methods, variables, constructors can be described inside enums as well.
The subsequent list explains the reserved words in Java. These hold-back words may not be employed as steady, variable, or any other identifier names.
abstract | assert | boolean | break |
byte | case | catch | char |
class | const | continue | default |
do | double | else | enum |
extends | final | finally | float |
for | goto | if | implements |
import | instanceof | int | interface |
long | native | new | package |
private | protected | public | return |
short | static | strictfp | super |
switch | synchronized | this | throw |
throws | transient | try | void |
volatile | while |
Java hold up single-line and multi-line remarks very alike to C and C++. Every character obtainable inside any comment is disregard by Java compiler.
public class MyFirstJavaProgram { /* This is my first java program. * This will print 'Hello World' as the output * This is an example of multi-line comments. */ public static void main(String []args) { // This is an example of single line comment /* This is also an example of single line comment. */ System.out.println("Hello World"); } }
Hello World
A line containing only white space, perhaps with a comment, is acknowledged as a blank line, and Java absolutely overlooks it.
In Java, classes can be obtained from classes. If you require creating a new class, then it is potential to obtain your new class from the by now offered code.
This concept permits you to use again the meadows and techniques of the obtainable class with no have to rewrite the code in an original class. In this situation, the obtainable class is called the superclass and the get class is called the subclass.
In Java language, a line can be characterized as a contract between objects on how to converse with each other. Crossing points play an essential role while it comes to the idea of legacy.
A border defines the methods, a derived class (subclass) should utilize. However, the execution of the systems is entirely up to the subclass.