What is 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.
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Output: Hello World
Example Explain:
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)
main()
method will be executedJust 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() :
what happens at compile time?
Java Variable:
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 |
Data types are divided into two groups:
- Primitive data types - includes
byte
,short
,int
,long
,float
,double
,boolean
andchar
- Non-primitive data types - such as String, Arrays and Classes
Primitive Data Types:
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 :
1. Byte :
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:
byte b = 10, byte a = -20;
2. Short :
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.
short s = 1000, short a = -2000;
3. Int :
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;
int num=100;
4. Long :
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;
long a=100000L;
5. Float :
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
float a = 526.5f
6. Double :
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
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'
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;
boolean isOk = true;
Non Primitive 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 variableExample :String str = “Universe”;
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"};
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
3. Class :
4. Interfaces :