INTRODUCTION
Java is well-known for its portability, which means that a Java program can run on any platform that includes a Java Virtual Machine (JVM). This simplifies the creation of programs that can operate on multiple platforms without having to rewrite the code for each one.
Java is commonly utilized in a range of areas, including finance, healthcare, and telecommunications. It is also widely used for developing enterprise-level applications and is a preferred choice for server-side development.
What is Java?
Sun Microsystems released Java as a programming language in 1995. James Gosling started it all. It is based on C and C++ syntax.
it is a programming language that is class-based, object-oriented, high-level, and secure
Here are some examples of how Java is used:-
- Java is used in the E-commerce and web applications space.
- Java
is also used in Financial Services. Companies like Goldman Sachs, Citigroup, and
Barclays use java.
- Java
is also used in Financial Services. Companies like Goldman Sachs, Citigroup, and
Barclays use java.
- Java
is used to develop Software tools like Eclipse IDE, IntelliJ Idea, and NetBeans
IDE.
Now let's write our first code of Java.
java.png
Explanation of code:
- Whatever code we create in Java must be placed in a class. If it is a
public class then file name should be same as class name.
- public:
public is a access modifier which tells us where we can access the variables
and all in code. In simple words it means visibility.
We will talk about it in depth later on
- JVM:
its full form is Java virtual machine. It creates an environment where java
code can compile. Due to this Java is machine independent
You can use the same Java code to run on any machine.
- static: Static is a keyword. Any method that is declared static is known as a static method. The primary advantage of the static technique
is that no object is required to activate the static method (as mentioned below). The JVM executes the main() method, therefore it does not need to be called.
To invoke the main() method, you must first create an object. As a result, it conserves memory.
- main: main is function name. Every class has many functions which have some code with some functionalities. main function is always the starting point of all program.
- String:
it is the data type for the 'a[]' array. There are other data types also available. we
will see them in just a while.
- void: It is the main function's return type.
- System.out.println("..."): Whenever we want to print anything on the console we have to use this command.
Where to write code and how to run it?
If you are a beginner it will be best if you use Notepad or
Notepad++ to run java codes. Else you can use many IDEs to run java like
IntelliJ Idea,
Netbeans, Eclipse, VScode etc.
To run the code save the file with name same as class name
and with extension.java . Then go to command prompt write javac <'your file
name'.java> click enter
then write java <file name> without extension hit
enter.
cmd prmpt.png
Note:
- Java
is case sensitive language so 'a' and 'A' are considered as different
- Before running the code, ensure that you have downloaded and installed jdk.
- here
you can notice I used cd desktop. Cd is used for changing directories. This command
is used to locate the directory in which our file is saved. If you save the file I
some other directory then do it accordingly.
Variables in JAVA
A
variable is like a container which stores value for you that you can use later
or at any time.
Variables can be of three types:
- Local
Variables: These variables can be accessed only inside the body where they are
declared. They cannot be defined static.
- Instance Variable: An instance variable is a variable declared within the class but outside the method body. It's not marked as static.
Static: A variable that is declared static is called a
static variable. It cannot be local. You can make a single duplicate of the static variable and distribute it to all instances of the class. Static variable memory allocation occurs only once when the class is loaded into memory.
Example
variable.png
What
is an object in Java?
An object is a type of instance that we utilize to call a function. An object is always created for a specific class. Then we can use that object to call any function of that class.
example:
Explanation of code: here we create a class 'object_example'
then we create a function 'method1' to call this function we create an object
O1 in the main method.
It is important to note that we create the object and call the function in the main method since every program begins with the main method.
what
is memory allocation?
When the JVM begins the program, the first thing it does is locate all objects and variables and assign their memory in the computer ram. This is known as memory allocation.
Variables and functions with static keywords must first have memory in RAM. Furthermore, memory has only been given to static variables and static functions once in one program.
As a result, they are memory efficient.
Data Types in Java
There are two types of variables:
int:
It is used to store integer values. Its default size is 4 bytes. That means for
every variable it will cover 4 bytes of space in ram.
float:
It is used to store decimal values. Its default size is 4 bytes. That means for
every variable it will cover 4 bytes of space in ram.
long:
It is used to store integer values. Its default size is 8 bytes. That means for
every variable it will cover 4 bytes of space in ram. This is the same as int but it has a larger range than int. That means whenever you are
required to store large numbers you will use long.
double:
It is used to store decimal values. Its default size is 8 bytes. That means for
every variable it will cover 4 bytes of space in ram. This is the same as float
char:
It is used to store a single character value like 'A', 'B' etc; Its default
size is 2 bytes.
boolean:
It is used to store either 'true' or 'false'. Its default size is 1 byte.
Non-primitive data types are user-defined data types.String: It's used to save a string of characters. like
any sentence or name etc. (will discuss it in an upcoming blog)
array: It is used to store several values. (This will be covered in future posts)
Example
for data types
Type Name
|
Bytes
|
Range of Values
|
int
|
4
|
-2,147,483,648 to 2,147,483,647
|
bool
|
1
|
false or true
|
char
|
1
|
-128 to 127 by default
|
short
|
2
|
-32,768 to 32,767
|
long
|
4
|
-2,147,483,648 to 2,147,483,647
|
float
|
4
|
3.4E +/- 38 (7 digits)
|
double
|
8
|
1.7E +/- 308 (15 digits)
|
long double
|
same as double
|
Same as double
|
Comments: Comments are that part of code that JVM will
ignore and not interpret as code. they are written as '//' If they were ignored by JVM
then why do we use comments. Comments are used to aid in the self-understanding of code or to make code more understandable to others.
So that's it for today and follow us for more...
thanks for reading...