Overview of Java
Java is a popular programming language developed by Sun Microsystems (now owned by Oracle), to develop mobile apps, web apps, desktop apps, games, and much more. Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
Key characteristics and features of Java:
1. Object-Oriented: In Java, everything is an object. Java can be easily extended as it is based on the Object model.
2. Platform Independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform-specific machines, but rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
3. Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
4. Secure: With Java’s secure feature, it enables to development of virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
5. Architecture-neutral: Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of a Java runtime system.
6. Portable: Being architecture-neutral and having no implementation-dependent aspects of the specification makes Java portable.
7. Robust: Java makes an effort to eliminate error-prone situations by emphasizing mainly compile-time error checking and runtime checking.
8. Multithreaded: With Java’s multithreaded feature, it is possible to write programs that can perform many tasks simultaneously.
9. Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere.
10. High Performance: With the use of Just-In-Time compilers, Java enables high performance.
11. Distributed: Java is designed for the distributed environment of the internet.
Java is used for developing a wide range of applications, from mobile applications (Android apps) to server-side applications for data centers, game applications, and more. It’s one of the most popular programming languages and is used by millions of developers worldwide.
First Java Code Example
- Install Java on Mac: https://www.java.com/en/download/help/mac_install.html
- Download IntelliJ: https://www.jetbrains.com/idea/download/#section=mac
To Run this code:
public class HelloWorld {
// main function to run the code
public static void main(String[] args) {
System.out.println("Hello World"); // → stdOut/stdErr
}
}
Compile the Main class: javac HellWorld.java , then run it: java HellWord. Or in Intellij, just click run.
Here,
– public class HelloWorld { … }: we’re declaring a class named HelloWorld. In Java, every application must contain a main class that wraps up all the program code. This class is made public which means it can be accessed from any other class.
– public static void main(String[] args) { … }: This is the main method of the class. Every Java application must have a main method. This is the entry point of any Java application, and the Java Virtual Machine (JVM) calls this method when the program starts. The keyword public denotes that this method can be accessed from anywhere. The keyword static means that this method belongs to the HelloWorld class and not to any instance of the HelloWorld class. void signifies that this method doesn’t return any value. String[] args are the parameters passed to the main method (command-line arguments).
– System.out.println(“Hello World”); : This line outputs the string “Hello World” to the console. System.out is an object that represents the standard output stream (typically the console), and println is a method on that object that prints a line of text to that stream.
When you run this program, it will print out the string “Hello World” to the console.
Basic Concepts of Java
Here are some of the basic concepts you should understand:
- Variables: A variable is a place to store values. In Java, every variable has a type, which tells what kind of value it can hold. For example, int for integers, double for real numbers, boolean for true/false, and String for text.
- Operators: Operators are symbols that perform operations on variables and values. For example, +, -, *, and / are arithmetic operators.
- Control Flow Statements: These are statements that control the flow of your program, such as if, else, for, while, and switch.
- Arrays: An array is a data structure that can hold a fixed number of values of the same type.
- Methods: A method is a block of code that only runs when it’s called. You can pass data into methods through parameters.
- Classes and Objects: A class is a template for creating objects. An object is an instance of a class and can contain both properties (data) and methods (functions).
Java Naming Style
Java has a standard convention for naming classes, methods, and variables, which is widely accepted and followed. Here are some of the most common conventions:
- Class names should be nouns, in mixed case with the first letter of each internal word capitalized – CamelCase. Try to keep your class names simple and descriptive. Example: Customer, StringBuilder, SystemColor, etc.
- Method names should be verbs, in mixed case with the first letter lowercase and the first letter of each internal word capitalized. Example: run(), getBackground(), printMessage(), etc.
- Variable names should be in mixed case with the first letter lowercase and the first letter of each internal word capitalized. Example: myVariable, backgroundColor, customerAddress, etc.
- Constant variables should be all uppercase with words separated by underscores (_). Example: MAX_SIZE, COLOR_RED, TEMP_DIRECTORY, etc.
- Package names should be in lowercase. If the name is composed of multiple words, they should be concatenated together (no underscores). Example: java.lang, java.util, org.apache.commons, etc.
- Type parameters (generics) should be single, uppercase letters. The letter ‘T’ is by convention the first choice for type parameters. Example: class Box<T> where T stands for “Type”.
These naming conventions make your code easier to read for you and others. It allows anyone to understand your code, its structure, and its function, faster and more accurately. It is always a good idea to stick to this standard naming convention, even though the Java language does not enforce these rules strictly.
Reference: