Basics of JAVA
JAVA PROGRAMMING
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
- Mobile applications (specially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games
- Database connection
- And much, much more!
Why Use Java?
- Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
- It is one of the most popular programming language in the world
- It is easy to learn and simple to use
- It is open-source and free
- It is secure, fast and powerful
- It has a huge community support (tens of millions of developers)
- Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
- As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa
Java Syntax
Program to Print Hello World :public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Every line of code that runs in Java must be inside a class. In our example, we named the class Main. A class should always start with
an uppercase first letter.Any code inside the main() method will be executed. You don't have to understand the keywords before and after main. You will get to know
them bit by bit while reading this tutorial. Java Comments
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line
is ignored by Java (will not be executed).
// This is a comment
System.out.println("Hello World"); Java Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by Java.
Example
/* The code below will print the words Hello World
to the screen,
and it is amazing */
System.out.println("Hello World");
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
String- stores text, such as "Hello". String values are surrounded by double quotesint- stores integers (whole numbers), without decimals, such as 123 or -123float- stores floating point numbers, with decimals, such as 19.99 or -19.99char- stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotesboolean- stores values with two states: true or false
Java Data Types
Data types are divided into two groups:
- Primitive data types - includes
byte,short,int,long,float,double,booleanandchar - Non-primitive data types - such as Strings ,Arrays , Classes
Java Operators
Operators are used to perform operations on variables and values.
Java divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bit-wise operators
Java If ... Else :
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
We can use these conditions to perform different actions :
ifto specify a block of code to be executed, if a specified condition is true-
elseto specify a block of code to be executed, if the same condition is false -
else ifto specify a new condition to test, if the first condition is false -
switchto specify many alternative blocks of code to be executed
Example
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
} Java While Loop
Thewhile loop loops through a block of code as long as a specified condition is true. Example
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Java For Loop
When you know exactly how many times you want to loop through a block of code, use thefor loop instead of a while loop.
Example
for (int i = 0; i < 5; i++) {
System.out.println(i); }
Comments
Post a Comment