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

Java 3

The document explains the differences between print() and println() methods in Java, highlighting that print() keeps the cursor on the same line while println() moves it to the next line. It also covers type casting, detailing the two types: widening (implicit) casting, which is automatic and safe, and narrowing (explicit) casting, which requires manual specification and may result in data loss. Additionally, it provides examples of type casting with characters.

Uploaded by

rojasri
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

Java 3

The document explains the differences between print() and println() methods in Java, highlighting that print() keeps the cursor on the same line while println() moves it to the next line. It also covers type casting, detailing the two types: widening (implicit) casting, which is automatic and safe, and narrowing (explicit) casting, which requires manual specification and may result in data loss. Additionally, it provides examples of type casting with characters.

Uploaded by

rojasri
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

1.

print() vs println() in Java

In Java, print() and println() are methods of the PrintStream class and are commonly used with
[Link].

1.1 [Link]()

Definition

print() displays the output without moving the cursor to the next line.

Syntax

[Link](value);

Example

class PrintExample {

public static void main(String[] args) {

[Link]("Hello");

[Link](" World");

Output

Hello World

👉 Notice: Both outputs appear on the same line.

1.2 [Link]()

Definition

println() displays the output and then moves the cursor to the next line.

Syntax

[Link](value);

Example

class PrintlnExample {

public static void main(String[] args) {

[Link]("Hello");

[Link]("World");

}
Output

Hello

World

👉 Each statement prints on a new line.

1.3 Difference Between print() and println()

Feature print() println()

Cursor movement Stays on same line Moves to next line

New line added ❌ No ✅ Yes

Usage Continuous output Line-by-line output

1.4 Special Case: println() with no argument

[Link]();

✔ This prints a blank line.

2. Type Casting in Java

2.1 What is Type Casting?

Type casting is the process of converting one data type into another.

Example:

int a = 10;

double b = a; // converting int to double

2.2 Types of Type Casting

Java supports two types of type casting:

1. Widening (Implicit) Casting

2. Narrowing (Explicit) Casting

3. Widening Type Casting (Implicit Casting)

Definition

Converting a smaller data type into a larger data type automatically.


✔ Done by Java
✔ No data loss
✔ No explicit syntax required

Order of Widening

byte → short → int → long → float → double

Example

class WideningExample {

public static void main(String[] args) {

int a = 10;

double b = a;

[Link](b);

Output

10.0

👉 int is automatically converted to double.

4. Narrowing Type Casting (Explicit Casting)

Definition

Converting a larger data type into a smaller data type.

❌ Not automatic
✔ Programmer must specify conversion
❌ Data loss may occur

Syntax

datatype variable = (datatype) value;

Example

class NarrowingExample {

public static void main(String[] args) {

double a = 10.75;

int b = (int) a;
[Link](b);

Output

10

👉 Decimal part .75 is lost.

4.1 Example Showing Data Loss

class DataLossExample {

public static void main(String[] args) {

int a = 130;

byte b = (byte) a;

[Link](b);

Output

-126

👉 Because byte range is -128 to 127, overflow occurs.

5. Type Casting with Characters

Example: char to int

class CharToInt {

public static void main(String[] args) {

char ch = 'A';

int a = ch;

[Link](a);

Output
65

👉 ASCII value of 'A' is 65.

Example: int to char

class IntToChar {

public static void main(String[] args) {

int a = 66;

char ch = (char) a;

[Link](ch);

Output

6. Summary for Students

print() vs println()

 print() → same line

 println() → new line

Type Casting

 Widening → automatic, safe

 Narrowing → manual, may lose data

You might also like