0% found this document useful (0 votes)
3 views6 pages

Imp Doc Java

The document outlines the basics of Java programming, focusing on keywords, variables, static typing, statements, primitive vs. reference types, and typecasting. It explains the significance of keywords as reserved words, the nature of variables as named memory locations, and the importance of static typing in error prevention. Additionally, it covers the differences between primitive and reference types, as well as the concepts of automatic and manual typecasting.

Uploaded by

annapurnag2262
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)
3 views6 pages

Imp Doc Java

The document outlines the basics of Java programming, focusing on keywords, variables, static typing, statements, primitive vs. reference types, and typecasting. It explains the significance of keywords as reserved words, the nature of variables as named memory locations, and the importance of static typing in error prevention. Additionally, it covers the differences between primitive and reference types, as well as the concepts of automatic and manual typecasting.

Uploaded by

annapurnag2262
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/ 6

✅ Java Basics: Keywords

📌 Concept:

 Keywords are reserved, predefined words in Java.

 There are 51 core Java keywords.

 Each keyword has a specific meaning recognized by the Java


compiler.

⚡ Important Points:

 Cannot be used as identifiers (e.g., variable, method, or class


names).

 Built-in: You can't redefine or change their meaning.

 Used to structure code logic, like declaring data types (int), control
flow (if), class structure (class, public, static).

 Learning them happens naturally through practice — memorization


not required upfront.

✅ Examples:

 int – declares integer variables

 if – conditional branching

 class, void, return, public – used in method and class definitions

✅ Java Basics: Variables & State Data

📌 Concept:

 State data refers to the information our application keeps track of


while it is running (e.g., user’s name, age).

 We store such data in variables.

⚡ Important Points:

 A variable is a named location in memory that holds data.


 The name (e.g., age) refers to a memory location where the value is
stored.

 The value in a variable can be updated at any time — hence the name
variable.

🧠 Assignment:

 Writing age = 42; assigns the value 42 to the variable age.

 The = sign is called the assignment operator.

⚠️Java Requires Type Declaration:

 Unlike some languages (e.g., Python), Java requires declaring the


data type before using a variable.

 Example:

 int age = 42; // ✅ Correct way to declare and assign an integer variable
in Java

age = 43; // ✅ No need to specify type again

 The keyword int specifies the type as integer.

 Once declared, you can reassign the variable without re-declaring its
type.

✅ Java Basics: Static Typing

📌 Concept:

 Java uses static typing, which means the data type is bound to the
variable when it's defined.

 The compiler checks type correctness at compile time.

⚡ Important Points:

 You cannot assign a value of a different type to a statically typed


variable.

 int age = 42;

age = "forty two"; // ❌ Compile-time error

 This helps catch type-related mistakes before the code runs.


 Once a variable is declared with a specific type, it must always store
that type of data.

🔄 Static vs. Dynamic Typing:

Feature Static Typing (Java) Dynamic Typing (Python)

Type Binding Bound to variable Bound to value

Type Check Time Compile-time Run-time

Can Change Type


❌ No ✅ Yes
Later?

Compile error for wrong May cause logic errors at


Example Issue
type run-time

🧠 Key Takeaway:

 Static typing in Java improves reliability and helps catch errors early.

 While dynamic typing offers flexibility, it can lead to unexpected


behavior if not handled carefully.

✅ Java Basics: Statements and Semicolons

📌 Concept:

 Java uses semicolons (;) to indicate the end of a statement.

⚡ Important Points:

 Every executable line in Java must end with a semicolon.

 Omitting a semicolon may cause Java to misinterpret multiple


lines as a single statement, resulting in a compile-time error.

🧠 Example:

int age = 42; // ✅ Correct

int year = 2024 // ❌ Missing semicolon - causes compile error

 Semicolons help the Java compiler parse and separate instructions


correctly.
✅ Java Basics: Primitive vs. Reference Types

📌 Concept:

 In Java, variables can hold primitive values or reference values.

⚡ Primitive Values:

 Represent simple, raw data (e.g., numbers, characters).

 Stored directly in memory.

 Java has 8 primitive types: int, long, short, byte, float, double, char,
boolean.

 Each primitive type has a fixed memory size:

o int = 4 bytes (~ ±2 billion range)

o long = 8 bytes (~ ±9 quintillion range)

⚡ Reference Values:

 A reference points to an object stored elsewhere in memory.

 Used with object types like String, Integer, arrays, and custom
classes.

 Example: Integer age = 42; creates an object, stores it in memory, and


the variable holds a reference like Integer@505.

🧠 Syntax Comparison:

// Primitive type

int age = 42;

// Object type

String text = "Hello there";

Integer number = 42;

🧠 Key Differences:

 Primitive types: Start with lowercase letters and store raw values.

 Object types: Start with uppercase letters and store references.

 Object types can include methods and additional data.


✅ Object Benefits:

 Come with built-in methods (e.g., text.length() for String).

 Allow custom behaviors and fields.

 Enable flexibility and reusability in complex applications.

✅ Java Basics: Typecasting

📌 Concept:

 Typecasting is converting a value from one data type to another.

 Java supports two kinds of typecasting:

o Automatic (Widening)

o Manual (Narrowing)

🔁 Automatic Casting (Widening):

 Happens implicitly, no developer action needed.

 Converts a smaller type to a larger type.

 Example:

int num = 3;

double newNum = num; // Automatically casted to 3.0

 No precision is lost.

✋ Manual Casting (Narrowing):

 Required when converting from a larger type to a smaller type.

 Done using parentheses with the target type.

 Example:

double number = 3.5;

int result = (int) number; // Manual cast: result = 3

 Precision is lost — decimal part is truncated.

 Without manual cast, Java will throw a type mismatch compile-time


error.
⚠️Note:

 Manual casting is also used when converting between object types.

 Always consider whether data might be lost or changed during casting.

You might also like