There are two types of programming languages. The low-level programming languages and high-level.
The low-level languages are very close to instructions that directly understandable by a machine. Sometimes, we refer to them as an assembly language or machine code.
In another hand, high-level programming languages have a syntax that understandable by humans. We know, machines can only understand binary code (0 and 1) so in order to run our program, we will need to first translate it into binary code. The process of translating our source code written in the high-level language to byte code called a compilation. A compilation is done by a special program — compiler. After, a compiled code can be executed by a computer.
Java is a high-level programming language. We will write source code with relatively understandable syntax, compile it, and then we will execute our compiled code.
Java Programming Language
In order to start writing code in Java, we need to install Java Development Kit (JDK) on our computer.
- Compiler. A Compiler for java will compile a source into a java-byte code. Java-byte code is not quite machine code. Then, java-byte code will be executed by Java Virtual Machine(JVM).
- Java Runtime Environment (JRE). It contains JVM, core java libraries.
- Tools for java development(archiver, docs generator, and etc).
As we mentioned above, the java compiler converts source code into java-byte code. Java-byte code it’s not yet machine code, though, it’s close to it. Then, JVM will convert java-byte code into machine code and execute it.
Why java has one more step and doesn’t convert directly to the machine code by a compiler?
Write once, run anywhere
Java is a platform-independent programming language. Let’s try to understand what does it mean and in which way it’s platform-independent.
To start, different hardware and Operating Systems require different machine codes. If we will come back to the first schema where we have
SOURCE CODE -> COMPILER -> MACHINE CODE
We can see compiler generates machine code. It means for different machines, we need different compilers. Compilers are complex programs and expensive to build.
Now, let’s take a look at a schema for Java
SOURCE CODE -> COMPILER -> JAVA-BYTE CODE -> JVM -> MACHINE CODE
We can see here, the compiler generates java-byte code and Java Virtual Machine(JVM) takes java-byte code and generates machine code. In Java, there is only one compiler for any machine but different JVMs. JVMs are less complex programs to build than compilers. This approach lets java adopt new environments faster.
In my opinion, we cannot say Java is %100 platform-independent. We will still need to install different JRE for different machines to execute our java program. For example, for Mac, you will need a different one than for Windows.
- Java file name should have the exact same name as a public class name.
- Java files have
javac <pathToJavaFileName>.javawill compile java source code and produce a file with
.classextension with java-byte code.
java <pathToJavaFileName>will run the compiled file.
Breaking down the HelloWorld program
- class is a blueprint for objects but for now, it’s useful to think that we just need a class to write a java program.
- the main method is a special method that actually runs our program. Everything in the main method will be executed when we run our program from top to bottom.
- java file can have only one public class, but many nonpublic classes.
All about print statements
System.out.println();will print in a new line(actually, it will add a new line after printing).
System.out.print();will print everything in one line.
- We can use
\to escape special characters in java.
\n— new line.
\t— tab space.
This article is part of the series of articles to learn Java programming language from Tech Lead Academy:1. Introduction to programming
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET