0% found this document useful (0 votes)
43 views5 pages

Day 1 and 2

Introduction to basic java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Day 1 and 2

Introduction to basic java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. Java vs.

JavaScript

 Java:
o A compiled and object-oriented programming language.
o Primarily used for building standalone applications and server-side
applications.
o Strongly typed (you must declare the type of variables before using them).
o Syntax is similar to C++.
o Java programs run in a Java Virtual Machine (JVM).
 JavaScript:
o A scripting language that runs inside the web browser.
o Primarily used for client-side functionality (such as form validation, dynamic
content).
o Loosely typed (you don’t need to declare types of variables).
o Runs in browsers without compilation; executed by JavaScript engine (e.g.,
V8 for Chrome).

2. Why Choose Java?

 Java is one of the most popular programming languages in the world.


 It supports Object-Oriented Programming (OOP), which models real-world problems.
 Java is platform-independent—programs written in Java can run on any machine
that supports the JVM.

3. Structure of a Java Program

// Hello World Program in Java

public class HelloWorld {

public static void main(String[] args) {

// Print statement

System.out.println("Hello, World!");

Explanation:

 public class HelloWorld: Declares a class called HelloWorld. Every Java program
needs at least one class.
 public static void main(String[] args): This is the main method where the program
starts execution.
 System.out.println("Hello, World!"): This prints "Hello, World!" to the console.

4. Steps to Compile and Run a Java Program


1. Write the code in a file named HelloWorld.java.
2. Compile the code using the command: javac HelloWorld.java.
o This generates a .class file containing bytecode.
3. Run the program using the command: java HelloWorld.
o The JVM interprets the bytecode and executes the program.

5. Java Terminologies

 Class: A blueprint for creating objects. It defines data and behavior.


 Object: An instance of a class. You can create many objects from one class.
 Method: A block of code that performs a specific task.
 Field: Variables within a class that represent the properties of an object.

Variables and Data Types

1. Variables

 A variable is a container that holds data that can be changed during the execution of a
program.
 Declaration: int x;
 Initialization: x = 5;

2. Data Types in Java

1. Primitive Data Types:


o int: Whole numbers (e.g., int x = 5;)
o double: Floating-point numbers (e.g., double y = 5.99;)
o char: Single characters (e.g., char letter = 'A';)
o boolean: True or false (e.g., boolean isJavaFun = true;)

2. Non-Primitive Data Types:


o String: A sequence of characters (e.g., String name = "Java";)
o Arrays, Classes, Interfaces, etc.

3. Examples of Variable Declaration and Initialization

public class Main {

public static void main(String[] args) {

int myNum = 15; // Integer

double myDouble = 5.99; // Floating-point number

char myChar = 'A'; // Character

boolean myBool = true; // Boolean

String myText = "Hello"; // String

}
4. Java Operators

 Arithmetic Operators: +, -, *, /, % (modulo)


o Example: int sum = 10 + 5; // sum = 15
 Relational Operators: ==, !=, >, <, >=, <=
o Example: if (a > b) {...}
 Assignment Operators: =, +=, -=, *=, /=, %=
o Example: x += 5; // Equivalent to x = x + 5;

Basic Java Program: Hello World!

Let's break down a simple "Hello World" program to understand the basics.

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!"); // Print statement

 Explanation:
o public class HelloWorld: This defines a class named HelloWorld. In Java, all
code must be part of a class.
o public static void main(String[] args): This is the main method, the
entry point where the program starts running.
o System.out.println(): This prints text to the console. It is a pre-built Java
method.

Java Comments

 Comments are ignored by the compiler and are used to provide explanations within the
code.
 Single-line comment: // This is a single-line comment
 Multi-line comment:

/*
This is a multi-line comment
*/
Integrated Development Environment (IDE) Setup

You can use an IDE to write, compile, and run your Java code more efficiently. Popular Java
IDEs include:

1. Eclipse: Feature-rich with auto-completion and debugging tools.


2. IntelliJ IDEA: Offers a modern interface and advanced development tools.
3. NetBeans: Good for beginners.

Steps:

1. Download and Install the JDK (Java Development Kit) from Oracle’s website.
2. Install an IDE (e.g., Eclipse or IntelliJ IDEA).
3. Create a new Java project.
4. Write code and run it directly from the IDE.

Practice Problems (Day 1–2)

1. Hello World Program:


o Write a program to print "Hello Students" on one line and "Jadavpur University" on
the next.

public class HelloStudents {


public static void main(String[] args) {
System.out.println("Hello Students");
System.out.println("Jadavpur University");
}
}

2. Simple Arithmetic:

 Write a program that takes two numbers from the user and outputs their sum,
difference, and product.

import java.util.Scanner; // Import the Scanner class

public class Arithmetic {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); // Create a Scanner object

System.out.println("Enter first number: ");

int num1 = sc.nextInt();


System.out.println("Enter second number: ");

int num2 = sc.nextInt();

int sum = num1 + num2;

int difference = num1 - num2;

int product = num1 * num2;

System.out.println("Sum: " + sum);

System.out.println("Difference: " + difference);

System.out.println("Product: " + product);

Day 1–2 Goals

 Understand the basics of Java syntax: Writing and running simple programs.
 Learn to differentiate Java from JavaScript.
 Get comfortable with variable declaration, data types, and performing arithmetic
operations.
 Set up your IDE and familiarize yourself with writing and running Java code.

You might also like