top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Java For Beginners Part -1

In this blog we will be discussed few basic topics about Java, Platform independence, Java features, OOPs concept, Java Variables, Data types in Java, Operators in Java, what are the conditional statements in Java ,how ,when and where we have to use that with examples it has explained. Loop concepts ,syntax, workflow also discussed.


What is java?

Java is a computer Programming Language that enforces an Object-Oriented Programming model.

Java is a Platform independent Programming Language that follows the logic "Write once, Run anywhere".

Java can be used to create complete applications that may run on a single computer or can be distributed among servers and clients in a network.


What is Platform independence?

The meaning of platform-independent is that the java compiled code(byte code) can run on all operating systems. Java compiler converts the programming code into byte code. Byte code is platform-independent and can be run on any processor or system.


Java Features

Simple, Secure, Portable, High Performance, Distributed, Object Oriented, Robust, Dynamic, Multi-Threaded.

Oops (Object - Oriented Programming)

Java is an Object Oriented Programming language. Everything is performed using "Objects". Java can be easily extended since it is based on the object model.

Java Comments

Single - line Comments - Start with two slashes.

Example : // This is a comment

Multi - line Comments - Start with /* and ends with */.

Example : /* 1. These are all comments */

Java Variables

Variables are containers for storing data values .These are the different types of variables.

. String - Stores text ,such as "Hello" String values are surrounded by double quotes.

. int - Stores integers ( whole numbers without decimals)

. float - Stores floating point numbers (with decimals)

. char - Stores single characters, such as 'a' Char values are surrounded by single quotes.

. Boolean - Stores values with two sates true or false.


Java Data Types

In java datatypes are divided into two groups.

1. Primitive data types

2. Non -Primitive data types(Derived data types)

1. Primitive data types

A primitive data type specifies the size and types of variable values, and it has no additional methods. These aren't considered objects and represent raw values.

There are eight primitive data types in java.

byte, short, int, long , float, double, boolean , char.


2. Non -Primitive data types(Derived data types)

Non-primitive data types are called reference types because they refer to objects. Non-Primitive data types are classified as Array, Class, String, and Interface. Arrays in Java are used to store elements of the same data type in a contiguous manner.


Java Operators

Operators are used to perform operations on variables and values.

Java divides the Operators into the groups some of few are

. Arithmetic Operators ( + , -, * , /, % , ++ ,- -)

. Assignment Operators (= , +=,-=, *=, /=,%=,&=, |=, ^=, >>=,<<=)

. Comparison Operators (==, !=, >, <, >=, <=)

. Logical Operators (&&, || ,!)

. Bitwise Operators (&, |, ^, ~)


Conditional Statements in Java

1. if statement

2. nested if statement

3. if-else statement

4. if-else-if statement

5. Switch Case Statement

1. If Statement

Use the if condition statement to specify a block of java code to be executed if a condition is true.

Syntax :

if (condition) {

statement(s);

}

Example :


2. Nested if Statement

If the first if condition is true then the section of code under first if condition would execute and it goes to the second if condition. If second if condition is true then the section of code under second if condition would execute.

Syntax :

if(condition1) {

statement1(s);

if(condition2){

statement2(s);

}

}

Example :


3. if-else statement

Use the else statement to specify a block of java code to be executed if a condition is false.

Syntax :

if(condition){

statement(s);

} else {

statement(s);

}

Example :


4. if-else-if statement

Use the else-if statement to specify a new condition if the first condition is false.

Syntax :

if(condition1){

statement(s);

} else if(condition2){

statement(s);

}

else{

statement(s);

}

Example :


5. Switch Case Statement

Use the switch statement to select one of many code blocks to be executed.

Syntax :

switch(expression) { case 1: //statement(s) break; case 2: //statement(s break; : : : default: //optional //statement(s) //This code will be executed if all cases are not matched }

Example :


Ternary Operator

Ternary operator provides an abbreviated syntax to evaluate a true or false condition, and return a value based on the Boolean result. This is a short-hand if else.

Syntax :

Variable = (condition) ? (return if true) : (return if false);

Example :

In above example we have used the same example what we used in if else . with this ternary operator we can easily reduce the lines of code.


Loops

Loops in Java is a feature used to execute a particular part of the program repeatedly if a given condition evaluates to be true. Loops are handy they save time, reduce errors, and they make code more readable.

For Loop

Java for loop helps execute a set of code repeatedly, and it is recommended when the number of iterations is fixed.

Syntax :

for( initializing ; condition ; increment/decrement) {

statement(s);

}

Example :

Here break statement used to jump out of this loop.

Continue statement help us to break(skip) one particular iteration after skipped this iteration it will continue with the next iteration.


For -each Loop

For-each loop is used on an array or a collection type. It works as an iterator and helps traverse through an array or collection elements, and returns them. While declaring a for-each loop, you don’t have to provide the increment or decrement statement as the loop will, by default, traverse through each element.

Syntax :

for(type variableName : arrayName) {

statement(s);

}

Example :


While Loop

While loop loops through a block of code as long as a specified condition is true. Do not forget to increase the variable used in the condition, otherwise the loop will never end.

Workflow

. First will check the test expression and then statements inside the while loop body are executed.

. will increase/decrease the value.

. if the test expression will be failed then the while loop terminates .

Example :


Output :



Do /While Loop

do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Workflow

. First statements inside the Do/while loop body are executed once . Then will check the test expression.

. if the test expression will be failed then the Do/while loop terminates .

Example :


Output :


Conclusion :

Java is one of the widely used programming languages. Java basic is a vast area so we cannot cover all topics in single blog. if we try also it will be lengthy and confusing. so I am continuing rest of few topics in my next blog (Java For Beginners Part -2 Link : https://www.numpyninja.com/post/java-for-beginners-part-2) .


I believe that you have learnt about few Java Basic topics and how to use all conditions and loops through this blog. Thank You For reading my blog.




30 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page