How to Code in Java
Ads by Google
Java is a good choice for a students first General-purpose programming language. It is easy to use and it is a Class based, Object-Oriented Language. Over 3 billion devices worldwide are running Java as it can be used for many purposes such as Desktop Software Application, Web Applications, Game Development etc.
In this article we are going to learn the basics of how to code in Java.
‘Hello World’ program:
Consider the following simple Hello world program.
This is a Java ‘Class’ named Hello. Class is a blueprint for a object. Each Java program must have a class whose name is same as the name of the file. In this case the name of the file is ‘Hello.java’.
The class begins with a Java keyword ‘public’. It is an Access modifier. The four access levels are as follows:
- Accessible to the package, default modifier, no keyword used.
- Accessible to the class, ‘private’ keyword.
- Accessible to the world, ‘public’ keyword.
- Accessible to the package and derived sub-classes, ‘protected’ keyword.
The ‘main’ is invoked when the program is executed, we can call other methods of the class from the ‘main’ method. The ‘main’ method accepts an array of strings.
There is only a single statement in our ‘main’ function. It prints the text ‘Hello World’ onto the console. ‘System’ is Java class from the core library, ‘out’ is an object and ‘println’ is a method that prints the argument provided onto the console. Which in our case is “Hello World”.
Comments in Java:
We can write single line comments using ‘//’ and multi-line comments by enclosing the comment between /* and */.
Basic Datatypes:
There are two datatypes in Java:
- Primitive Data Types.
- Reference/Object Data Types.
Primitive Data Types:
There are 8 datatypes supported by Java.
- Byte
- short
- int
- long
- float
- double
- boolean
- char
We can use these datatypes to declare variables in the following way:
Conditionals:
Conditionals allow for decision making. Certain pieces of code to be only executed if a certain condition is true, if it is false a different set of statements would be executed.
Below is a Example of a simple java script program that uses if-else conditional:
We can also use a switch conditional. A sample Java script program is as Follows:
Loops:
Loops allow for executing a Java statement or set of statements multiple times.
A sample program utilizing the For loop is as follows:
Output:
A sample program using While loops to calculate the factorial of a number:
Output:
What do you need to code in Java?
Is it easy to learn Java?
Where do you write Java code?
How can I teach myself Java?
- Learn the Basics. As with anything, knowing the basics about Java is the best place to start.
- Practice Coding. To use the old cliché, practice makes perfect.
- Set Your Algorithm Carefully.
- Trace Your Codes on Paper.
- Read Sources on Java Programming Regularly.
Can I learn Java in 15 Days?
Can I learn Java in 3 months?
How quickly can I learn Java?
Can I learn Java per month?
Can I learn Java same day?
How can I learn Java in one hour?
Can I learn Java in a week?
Which programming language should I learn first?
Ads by Google