Java Basic Concept

 What is Java?

Java is a platform as well as a programming language. Java is a high-level programming language which is also robust, object-oriented, and secure.

Java was created in 1995 by Sun Microsystems (which is now a division of Oracle). The founder of Java, James Gosling, is known as the "Father of Java." It was known as Oak before Java. Because Oak was already a recognized business, James Gosling and his team decided to change the name to Java.

Basic Concept in java
Basic Concept in java


Why Use Java?

  • Java can be used on a variety of platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
  • It is one of the most widely used programming languages on the world. 
  • It's free and open-source.
  • It is safe, quick, and powerful.
  • It has a large support in the community (tens of millions of developers)
  • Java is an object-oriented programming language that offers programmers a logical    structure and allows code to be reused, saving development costs.
  • Java is similar to C++ and C#, making it simple for programmers to transfer from one to the other.

Application:

  • Desktop Applications 
  • Web Application
  • Enterprise Application (Banking application)
  • Game
  • Mobile Application
  • Robotics ,etc

Java First Example:

package basicProgram;

public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");

}

}
Output: Hello World


Example Explain:


Every line of Java code must be placed within a class. We named the class HelloWorld in our example. The first letter of a class should always be uppercase.

Note that Java is case-sensitive, so "HelloWorld" and "helloworld" are not similar.

The java file must have the same name as the class. When saving the file, use the class name as the filename and add ".java" to the end. To execute the given example on your machine,

Main Method:

The main() method is required in every Java code

public static void main(String[] args)
Any code inside the main() method will be executed
Just keep in mind that every Java program must have a class name that matches the filename, as well as the main() method.

System.out.println() :

We can use the println() method to print a line of text to the screen inside the main() method:


what happens at compile time?

The Java file is compiled by Java Compiler at compile time, which converts the Java code into bytecode.



Java Variable:

Variables are storage containers for data values.
  • String - stores text, such as "Hello". String values are surrounded by double quotes
  • int - stores integers (whole numbers), without decimals, such as 123 or -123
  • float - stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
  • boolean - stores values with two states: true or false


Java Data Types:

A data type is a  type of data. If the value "1.25" is entered into the variable "var1," the variable is formed as a floating point data type.

Data Types 

Data types are divided into two groups:

  • Primitive data types - includes byteshortintlongfloatdoubleboolean and char
  • Non-primitive data types - such as StringArrays and Classes 

Primitive Data Types:

The language specifies a primitive type and names it with a reserved keyword. The state of primitive values is not shared with other primitive values.

Data Type

Default Value

Default size

boolean

false

1 bit

char

'\u0000'

2 byte

byte

0

1 byte

short

0

2 byte

int

0

4 byte

long

0L

8 byte

float

0.0f

4 byte

double

0.0d

8 byte    














Integers :

    1Byte

From -128 to 127, the byte data type can store entire values. When you know the value will be between -128 and 127, you can use this instead of int or other integer types to save memory:

Example :
byte b = 10, byte a = -20;

 2Short : 

A 16-bit signed two's complement integer is the short data type. It has a value range of -32,768 to 32,767. (inclusive). It has a minimum of -32,768 and a maximum of 32,767. It has a value of 0 by default.

The short data type, like the byte data type, can be used to save memory. A short data type is twice the size of an integer.

Example :
short s = 1000, short a = -2000;

3Int : 

A 32-bit signed two's complement integer is defined by the int data type. Its range of values is - 2,147,483,648 (-231 -1) to 2,147,483,647 (231 -1). (inclusive). It has a minimum of 2,147,483,648 and a maximum of 2,147,483,647. It has a value of 0 by default.

Unless there is a memory constraint, the int data type is usually chosen as the default data type for integral values.

Example :
int num=100;


4Long : 

A 64-bit two's complement integer is the long data type. It has a value range of -9,223,372,036,854,775,808(-263 -1) to 9,223,372,036,854,775,807(263 -1). (inclusive). Its lowest and maximum values are 9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. It has a value of 0 by default. 

When you need a larger range of values than int ,than you should use the long data type.

Example :
long a=100000L;


5Float : 

A single-precision 32-bit IEEE 754 floating point is represented by the float data type. It has a limitless value range. If you need to preserve memory in big arrays of floating point integers, a float (rather than a double) is recommended. 

For accurate numbers such as cash, the float data type should never be used. It is set at 0.0F by default.

Example :
float a = 526.5f


6Double : 

A double data type is a 64-bit IEEE 754 floating point with double precision. It has an infinite value range. Like float, the double data type is commonly used for decimal numbers.

Example :
double d1 = 15.3


7. Char : 

A single 16-bit Unicode character is represented by the char data type. It has a value range of 'u0000' (or 0) to 'uffff' (or 65,535 inclusive). Characters are stored using the char data type.

Example :
char name = 'A' 


8. Booleans : 

The boolean keyword is used to specify a boolean data type, which can only take the values true or false:

Example :
boolean isOk = true;


Non Primitive Data Types:

Programmers create non-primitive data types. In Java, they aren't predefined like primitive data types. These data types are used to store several values or a group of values.

for example, an array. It has the ability to store a set of values. Another example is a class that can hold a variety of values. As a result, in Java, these data types are referred to as advanced data types.

                        

1. String : 

A string is a collection of characters, such as India, ABC123, and so on. The most basic technique to make a string object is to store a series of characters in a string type variable

Example :
String str = Universe;


2. Array : 

In Java, an array is an object that holds multiple variables of the same type. The data types of these variables might be primitive or non-primitive.

Example :
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


3. Class : 

In Java, a class is a blueprint that holds all of your data. To define an object's functionality, a class contains fields (variables) and methods.

4. Interfaces : 

An interface can include methods and variables, just like a class, but the methods specified in the interface are abstract by default (only method signature, no body).

 Next : Control Statements

Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post