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

Day1 Notes

The document provides an introduction to Java, a high-level, object-oriented programming language known for its platform independence. It covers Java's data types, including primitive and non-primitive types, variables, and how to use the Scanner class for user input. Additionally, it explains arrays in Java, their features, and syntax for declaration and initialization.

Uploaded by

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

Day1 Notes

The document provides an introduction to Java, a high-level, object-oriented programming language known for its platform independence. It covers Java's data types, including primitive and non-primitive types, variables, and how to use the Scanner class for user input. Additionally, it explains arrays in Java, their features, and syntax for declaration and initialization.

Uploaded by

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

Day1: 25th Jan, 2025

Installed JVK and VS code IDE.

a. What is Java?

 Java is a high-level, object-oriented programming language.


 It is platform-independent and follows the "Write Once, Run Anywhere" (WORA)
principle.

First Java Program:

Data Types in Java

Java data types are divided into Primitive Data Types and Non-Primitive Data Types.

1. Primitive Data Types

Primitive data types are predefined by the language and stored directly in memory. They are
lightweight and simple.
 int is commonly used for integers unless memory optimization is required.
 Use float for decimal numbers when memory is a concern, but double is more precise.
 char holds a single character enclosed in single quotes (e.g., 'A').
 boolean is used for true/false values in conditional statements.

2. Non-Primitive Data Types

Non-primitive (or reference) data types are created by the programmer and include objects,
arrays, and strings.

 String: Represents a sequence of characters.

Ex: String name = "Hello, World!";

 Array: Used to store multiple values of the same data type.

Ex: int[] numbers = {1, 2, 3, 4, 5};

 Classes and Objects: Defined by the programmer to model real-world entities


class Student {
String name;
int age;
}

Variables:

A variable is a container that holds data that can be changed during program execution. In Java,
variables have a type, which determines what kind of data they can hold.

a. Local Variables
 Declared inside a method, constructor, or block.
 Only accessible within the block where they are defined.
 Must be initialized before use.

b. Instance Variables

 Declared outside any method but inside the class.


 Belong to an instance of the class (object).
 Default values are provided if not initialized (e.g., 0 for int, null for objects).

c. Static Variables

 Declared using the static keyword.


 Shared among all instances of the class.

Write a program to take input using Scanner and print it


import java.util.Scanner; : This imports the Scanner class from java.util package. The
Scanner class is used to take user input from the console.

 Scanner scanner = new Scanner(System.in); :


 This creates a Scanner object named scanner.
 System.in refers to the standard input stream (the console), allowing the
program to read user input.

String name = scanner.nextLine();:

 Reads the user's input from the console as a String and stores it in the variable name.

 nextLine() is a method in the Scanner class that reads an entire line of input, including
spaces.

scanner.close(); :

 Closes the Scanner object to release system resources.

 This is a good programming practice, although not strictly necessary for small programs.
Introduction to Arrays in Java

An array is a collection of elements, all of the same data type, stored in contiguous memory
locations. Arrays are used to store multiple values in a single variable instead of declaring
separate variables for each value.

Features of Arrays:

1. Fixed Size: The size of an array is fixed at the time of its declaration.
2. Indexed: Elements are accessed using indices, starting from 0 to n-1 (where n is the size of the
array).
3. Homogeneous Elements: All elements in an array must be of the same data type.
4. Random Access: You can access any element directly using its index.

Syntax:

dataType[] arrayName; // Recommended

OR

dataType arrayName[]; // Also valid

arrayName = new dataType[size]; Initialisation

dataType[] arrayName = {value1, value2, value3, ...};  Combined Declaration,


Instantiation, and Initialization

You might also like