Why I Chose Java as My First Programming Language?
Out of the trio of Python, C++ and Java, Java is more of a balanced guy.
Python is very useful for stuff like machine learning and AI whereas C++ is useful in application that needs a speedy response and so does Java has its application in web and app designing.
But as a newbie in the field of programming and to learn the base concept of DSA, Java is a good option as it is neither like Python which has very easy syntax nor like C++ which doesn't has useful libraries and stuff like Garbage Collector thus saving a lot of time in learning basics and DSA also giving the flexibility of easily shifting to any of the languages with little more effort.
But in the end, it depends on the personal choice of choosing a language because what matters is that we learn all concepts and can properly apply them in real-world situations.
JDK-JRE-JVM (The Trio)
So basically, JVM or Java Virtual Machine is an abstract machine that turns bytecode ( this is generated by java compiler [.java -----> .class]) into native machine code or the code which your computer CPU can understand.
JRE or Java runtime environment is the superset of JVM containing Libraries and JVM in it.
JDK or Java Developmental Kit is the biggest set that contains JRE and other developmental instruments such as compiler, debugger, some Java docs etc.
Finally, the presence of bytecode makes Java "Write once and Run anywhere" i.e. Java program could be developed on any device, compiled into standard bytecode and be expected to run on any device equipped with a Java virtual machine.
First CodeπΆ
public class First
{
public static void main(String[] args)
{
System.out.println("Hello Readers")
//Guess the outcome
}
}
"System.out.println()" helps to print the text(needs to be contained in "") and number(doesn't need "") as the output of the code.
'//' helps one to comment on a single line that will be ignored by the compiler.
(Note - We will learn more about the words like public, static, and void later on.)
Also, it is important to develop a habit of writing clean code so that's why certain conventions will help you to write clean code. For all the naming conventions, click here.
Data Types -
Primitive -
Integer(int) - 4 bytes
long - 8 bytes (put l at the end)
short - 2 bytes
byte - 1 byte
float - 4 bytes (put f at the end)
double - 8 bytes (put d at the end)(default in Java)
Character(char) -- 2 bytes (Unicode)(has to be in ' ')
Boolean(boolean) -- true or false
Non Primitive -
String
Arrays
Classes
Interface
etc........
Some basic Differences --
Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and are not defined by Java (except for
String
).Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
A primitive type has always a value, while non-primitive types can be
null
.A primitive type starts with a lowercase letter, while a non-primitive type starts with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.
for more on this, click here.
Data Conversion and Casting
Descending order of Data type size -->
double
> float
> long
> int
> char
> short
> byte
- Conversion(Widening Casting) - occurs automatically and includes the conversion of the smaller type to the larger type.
public class Main
{
public static void main(String[] args)
{
int val = 9;
double Dval = val; // Automatic casting: int to double
System.out.println(val); // Outputs 9
System.out.println(Dval); // Outputs 9.0
}
}
- Casting(Narrowing Casting) - has to be done manually as in this we convert large types into small ones which also leads to some form of data loss. In this, we have to put the type in parenthesis in front of the value which is going to be converted.
public class Main
{
public static void main(String[] args)
{
double Dval = 9.78d;
int val = (int) Dval; // Manual casting: double to int
System.out.println(Dval); // Outputs 9.78
System.out.println(val); // Outputs 9
}
}
Conclusion -
So that's much for today. Hope You liked it
In the next blog we will learn topics -
Operator
If else, For, While loop etc.
Thanks for reading till the end, if you have any kind of suggestions just comment be down, and I'll catch up with you.ππ