Hello!
Welcome to my blog for my youtube channel Stick2Tech!
Today we will be talking about Java fundamentals designed for a beginner coder . This will give the basic technicalities of Java Programming.
1. Basics of Java Environment
Key terms to know about Java are:
a. IDE - refers to Integrated Development Environment which is an environment where developers can write Java code. An example of a Java development environment is Eclipse.
b. JDK – refers to Java Development Kit is a package which provides the environment to develop and execute Java programs. JDK is only used by Java Developers.
c. JRE – refers to Java Runtime Environment is a package that provides an environment to run the java application (classes).
d. JVM – refers to Java Virtual machine is a very important part of both JDK and JRE because it is contained in both the packages. JVM executes java programs line by line. This is why it is also known as an interpreter.
2. Java Compiler
Java compilers compile(or change the format of the code so it can be interpreted by the OS system) the code to produce Byte Code (which is essentially 1s and 0s) that can run only in JVM. Other languages like C++ compile code to machine level instructions that can be executed directly on the hardware.
An advantage of Java Byte code is that it can run in Java Virtual Machine in any Operating System. So, it can be built once and run anywhere, the byte code being interpreted differently depending on the OS system. Languages that compile code directly to the machine level instructions can only work in one operating system per compiled code.
A disadvantage of java classes (Byte Code) running in JVM is that it will be slower than the code that is compiled to machine level instructions because it is translated to byte code and then put into machine level instructions.
3. JVM Internal
JRE Uses JVM to run Java Program.
In this example, Hello.class is loaded in JVM by a class loader that has Byte Code. The JVM interpreter interprets Byte code and sends it to Runtime that does Just in time compilation(compiling to the machine level AS SOON as the byte code is complied) to Machine Level Instructions to be sent to Computer Hardware to run. In this case, Hello + Name will be displayed
4. Typical Java Program
As we have discussed above, classes consist of statements
Each statement ends with a semicolon
The compiled statement gives some instruction to Java Virtual Machine to perform some task
Parameter going in a method helps to pass some information to perform a task
The output of a method is the result of a method.
Comments are used to ignore statements. When a statement is ignored it is not considered in the overall code. There are three ways to indicate comments
// Use two backslashes to ignore anything after it till the end of the line
/* …. */ use backslash plus star to ignore all the text within /* backslash plus star to the end of */ star plus backslash
Java has a tool that produces a HTML document to quickly find the methods and other important facts about the classes. To produce this document there is a special type of comment
/** …. **/ using backslash plus double star to ignore all the text within /** backslash plus double star to the end of **/ double star plus backslash. The difference is that it will also document anything within that text in java documentation for that class.
5. What is a class and what is an instance or an object of a class?
“A class is the blueprint from which individual objects are created.”[1]
Example of class and Object is shown in this picture:
In the real world, you'll often find many individual dogs. Each dog has the same set of blueprints and therefore contains the same features like eye, nose, etc. In object-oriented terms, we say that one dog is an instance of the class of objects known as dog.
In this example, the class dog has two attributes called color and breed. This class is used in the DogShow by creating two instances of the dog competing in the DogShow competition. Then the method PrintDog is used to print the participating dog.
5. What is a package?
The package uses the reverse name pattern of the domain name to organize Java Classes (ex the org file is within the scr or source file, the utility file is within the org file, and the common file is within the utility file, and the commons file gives access to the Error log.java and ErrorLog.class).
In this example, the ErrorLog class is a part of org.utility.common. The common utility in that organization can be placed in that package.
The package impacts the file path structure in the drive.
6. What is a variable?
Variable is a name that is defined in a class that holds data. In this example, isSuccessful is a boolean type of variable that can be assigned true or false. So, the variable name remains the same but the value varies in a variable.
7. What is a data type?
Each variable has a data type that defines what type of data it can hold.
In the above examples: boolean(true and false), int(integer values), char(single characters)( are data types.
8. Primitive data types
There are 8 different primitive data types in java: Byte, short, int, long, float, double, Boolean, char.
Byte, short, int and long hold Integer values meaning that even when calculating them in a way where it would normally result in an integer value, it would instead express the value without the decimal.
These data types help in allocating proper space for variables to hold values.
9. Mathematical operators
There are 7 different types of mathematical operators in Java: Add, Subtract, Divide, Multiply, Modulus or Mod - expressed by the % sign, determines the remainder when 2 values would be divided, for example, 8%3 would equal 2 because 8 divided by 3 would have a remainder of 2; Increment, when you increment a value, you put the variable name that contains a numeric value then ++, and it increases that value by 1; Decrement, when you decrement a value, you put the variable name that contains a numeric value then --, and it decreases that value by 1.
10. Hands-on Java Development
I encourage you to try to do the following
1. Write HelloWorld program in Java, which is when you print out the message “hello world” in Java, you need to use a built-in function called System.out.println() then print out your message in double quotes
2. Add comments
3. Import a library
4. Create a package
5. Define a method
6. if using a desktop compiler, compile and look at the generated class
7. Run the program
Thank you for reading my blog post about java fundamental today. I will be posting other blogs so I would like to hear what you think I should talk about? Should I expand upon this topic, or is there something else you want me to cover, if this topic interested you, I encourage you to do your own research on the subject, or write in the comments below if you have any questions for me.
Acknowledgments:
[1]https://www.w3schools.com/
[2]https://docs.oracle.com/javase/tutorial/java/concepts/class.htm
[3]https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
Comentários