0% found this document useful (0 votes)
2 views2 pages

Java - Assignment - Solution 2

The document explains the differences between char and String in Java, highlighting their nature, syntax, and behavior. Char is a primitive data type representing a single character, while String is a reference data type representing a sequence of characters. The document also includes Java code examples demonstrating declaration, concatenation, and the behavior of both types.

Uploaded by

abademaha
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)
2 views2 pages

Java - Assignment - Solution 2

The document explains the differences between char and String in Java, highlighting their nature, syntax, and behavior. Char is a primitive data type representing a single character, while String is a reference data type representing a sequence of characters. The document also includes Java code examples demonstrating declaration, concatenation, and the behavior of both types.

Uploaded by

abademaha
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

ASSIGNMENT SOLUTION

Topic: Differences Between char and String in Java

Theoretical Part: Differences Explanation

In Java, char and String are two distinct fundamental types used to represent textual data, but they
differ significantly in nature, syntax, and behavior. Below is a detailed comparison based on the required
criteria:

Feature char String

Primitive data type. It represents


Reference data type (an Object). It
Data Type Nature a single 16-bit Unicode
represents a sequence of characters.
character.

Uses lowercase keyword char. Uses capitalized class name String.


Declaration Syntax Example: Example:
char letter = 'A'; String text = "Hello";

Must always be enclosed in Must always be enclosed in double


Quotation Marks
single quotes (' '). quotes (" ").

Variable size depending on the


Memory Size Fixed size of 2 bytes (16 bits). number of characters in the
sequence.

Using the + operator with


numbers can perform Using the + operator always performs
Concatenation
arithmetic addition using textual concatenation, converting
Behavior
ASCII/Unicode values, unless the other operand into a string.
concatenated with a String.

Practical Part: Java Code Examples

The following program demonstrates the declaration of both data types and showcases how string
concatenation behaves when combined with characters, strings, and numbers.

public class AssignmentSolution {


public static void main(String[] args) {

Page 1 of 2
// 1. Declaration Syntax and Quotation Marks
char myChar = 'J'; // Single quotes for a single character
String myString = "Java"; // Double quotes for a sequence of characters
int myNumber = 2026;

[Link]("--- 1. Variable Values ---");


[Link]("Character value: " + myChar);
[Link]("String value: " + myString);

// 2. String Concatenation with a Number


[Link]("
--- 2. Concatenation with a Number ---");
String stringAndNumber = myString + " Version " + myNumber;
[Link]("Result (String + Number): " + stringAndNumber);

// 3. Character Concatenation with a Number


[Link]("
--- 3. Behavior of char vs String with Numbers ---");

// Note: char + int performs mathematical addition based on Unicode values!


// 'J' has a Unicode value of 74. So 'J' + 5 = 74 + 5 = 79
[Link]("Direct char + number (myChar + 5): " + (myChar + 5));

// To concatenate a char as text, it must be part of a String context first


[Link]("Concatenated char as text: " + "" + myChar + myNumber);
}
}

Page 2 of 2

You might also like