Lecture 1 - First Program in Java

Java is a high-level, versatile, and widely used programming language. It was developed by Sun Microsystems (now owned by Oracle Corporation) and first released in 1995. Java is known for its platform independence, which means that programs written in Java can run on various computer systems without modification, as long as there is a compatible Java Virtual Machine (JVM) available for that system.


IMPORTANT:
👉No programming experience is required for this tutorial but if you have some programming experience in some other programming languages then obviously it will be beneficial.

👉It is strongly recommended to watch the youtube channel for better understanding of the topics/concepts discussed through out in this tutorial.


Key characteristics and features of Java include:

  • Object-Oriented: Java is primarily an object-oriented programming (OOP) language, which means it emphasizes the use of classes and objects to model and solve real-world problems.
  • Platform Independence: Java applications are compiled into an intermediate form called bytecode, which can be executed on any system with a compatible JVM. This "write once, run anywhere" feature is one of Java's most significant advantages.
  • Automatic Memory Management: Java uses a garbage collector to automatically manage memory, making it easier for developers by eliminating many common memory-related errors like memory leaks.
  • Rich Standard Library: Java comes with a vast standard library (Java Standard Library or Java API) that provides pre-built classes and methods for common tasks, making development more efficient.
  • Multi-threading: Java supports multi-threading, allowing developers to create programs that can execute multiple tasks concurrently, enhancing performance in applications that require parallel processing.
  • Security: Java has built-in security features that protect against various forms of malware and unauthorized access. Applets, for example, were once a common way to run Java programs within web browsers in a sandboxed environment.
  • Portability: Java applications can run on various operating systems, including Windows, macOS, Linux, and more, as long as the JVM is available for that platform.
  • Backward Compatibility: Java strives to maintain backward compatibility, ensuring that older Java applications continue to work on newer Java platforms with minimal modification.

Java is used in a variety of applications, including web development (using Java EE and Spring Framework), Android app development, enterprise software development, scientific computing, and more. It has remained a popular choice among developers for decades due to its versatility, reliability, and extensive ecosystem.

How to Install Java?

  1. Goto https://www.java.com/download/ie_manual.jsp. You will get a screen as below:


  2. Click on the "Download Java" button. It will ask the folder where you want to save the Java setup file after downloading.
  3. Click on the setup file once downloaded. It will install Java. No need to make any change during the installation process.

IDE (Integrated Development Environment) for Java


An IDE (Integrated Development Environments) is a software application or environment that provides a comprehensive set of tools and features for computer programmers and developers to create, edit, compile, run, test, and debug their software projects. There are several  IDEs available for Java development, each with its own set of features and capabilities but I will prefer NetBeans. It's an open-source IDE that provides a range of features and tools specifically tailored for Java developers.

How to Install NetBeans?

  1. Goto https://netbeans.apache.org/download/index.html. You will get a screen as below. Download the latest version of Netbeens given on the website, At the time of writing this blog,  the latest version is 'Apache NetBeans 18'  released on May 30, 2023. 


  2. Click on the "Download" button. It will display a screen as below:



  3. Click on the link mentioned in the above image.It will display a screen as below:

  4. Click on the link mentioned in the above image. It will ask the folder where you want to save the setup file after downloading.
  5. Click on the setup file once downloaded. It will install NetBeans. No need to make any change during the installation process.

Your First Program in Java

Once you have installed Java and NetBeans (or any other IDE for Java), now you are ready to write your first program in Java. A basic Java program has a specific structure that includes several essential components. Here's a typical structure for a simple Java program:





Let's break down the structure of a Java program:

Import Statements (Optional): At the beginning of your Java program, you can include import statements to bring in classes and packages from Java's standard library or other external libraries. These statements are optional but are used to access predefined classes or methods (functions) that your program may need.

Class Declaration: Every Java program is organized into classes. The public class MyClass part declares a class named MyClass. The name of the class should match the name of the file containing the code, with the .java extension. For example, if the class is called MyClass, the file should be named MyClass.java.

Main Method: In Java, the public static void main(String[] args) method serves as the entry point of the program. When you run your Java program, the JVM (Java Virtual Machine) starts executing the code from this method. This method is required in every Java program.

Program Logic: Within the main method, you write the core logic of your program. This is where you define variables, perform calculations, call methods, and carry out the tasks your program is meant to accomplish.

Output Statements: You can use the System.out.println() method to display output to the console. This is useful for debugging and for providing information to the user. In the example above, it prints "Hello, World!" to the console.

Comments: Comments are used to add explanations and documentation to your code. They are not executed by the program and are for the benefit of developers. In the example above, // is used for single-line comments, and /* */ is used for multi-line comments. In Java, comments are used to provide explanations, documentation, and notes within your code. Comments are ignored by the Java compiler and have no impact on the program's execution. They exist solely to make your code more readable and understandable for developers. Java supports both single-line and multi-line comments. Here's how you can use comments in Java:

Single-Line Comments: Single-line comments start with two forward slashes (//) and continue until the end of the line. Everything following // on that line is treated as a comment.

// This is a single-line comment int x = 5; // This comment explains the purpose of the following code

Multi-Line Comments: Multi-line comments are enclosed between /* and */. You can use them for longer explanations or to comment out entire sections of code.

/* This is a multi-line comment */

Remember that this is a basic structure, and Java programs can become much more complex as they grow. However, this basic template provides the foundation for any Java program. The main method is where execution begins, and you build your program's logic from there, using classes, methods, and variables as necessary to achieve your goals.

Next Lecture: How to Take Input From User

Further Readings:

👉 Why Java is called Java?

👉 Who developed Java?

👉 What kind of applications are developed in Java?

👉 What is a high-level language?

👉 What does compiler mean?

👉 What are syntax errors?

👉 What is debugging?

👉 What is source code?

👉 What is object code?

👉 What is byte code?

👉 What are keywords?

👉 What are identifiers?







Comments

Popular posts from this blog

Lecture 2 - How to Take Input From User in Java

Programming Conventions in Java

Why Java is Called Java?