C
JV

C to Java

10 lessons

Progress0%
1Introduction to Java2Data Types3Strings4Arrays and Collections5Object-Oriented Programming6Exception Handling7Collections and Generics8Modern Java Features9Interfaces and Polymorphism10Threads and Concurrency
All Mirror Courses
C
JV
Strings
MirrorLesson 3 of 10
Lesson 3

Strings

String handling in Java

Introduction

In this lesson, you'll learn about strings in Java. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with string handling in java.

JV
In Java:

Java has its own approach to string handling in java, which we'll explore step by step.

The Java Way

Let's see how Java handles this concept. Here's a typical example:

JV
Java Example
String s1 = "Hello";
String s2;

int len = s1.length();
s2 = s1;  // reference copy
String s3 = s1 + " World"; // concatenation
int cmp = s1.compareTo(s2);
boolean eq = s1.equals(s2);

// Immutable! Creates new string
String modified = s1.replace('H', 'J');

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <string.h>

char s1[50] = "Hello";
char s2[50];

int len = strlen(s1);
strcpy(s2, s1);
strcat(s1, " World");
int cmp = strcmp(s1, s2);

// Mutable
s1[0] = 'J'; // "Jello World"
Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

String is a class with methods

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Strings are immutable - operations create new strings

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Use .equals() for comparison, not ==

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Concatenation with + creates new String

Step-by-Step Breakdown

1. String is a Class

In Java, String is a full-featured class with many useful methods.

JV
Java
String s = "Hello World";

s.length();              // 11
s.charAt(0);             // 'H'
s.substring(0, 5);       // "Hello"
s.toUpperCase();         // "HELLO WORLD"
s.toLowerCase();         // "hello world"
s.trim();                // remove whitespace
s.split(" ");            // ["Hello", "World"]
s.contains("World");     // true
s.startsWith("Hello");   // true

2. String Comparison

Use .equals() for content comparison. == compares references!

C
C
strcmp(s1, s2) == 0
JV
Java
// WRONG - compares references
if (s1 == s2) { ... }

// CORRECT - compares content
if (s1.equals(s2)) { ... }

// Case-insensitive
if (s1.equalsIgnoreCase(s2)) { ... }
Common Pitfall
== on strings checks if they're the same object, not if they have the same content. Always use .equals().

3. StringBuilder

For building strings efficiently, use StringBuilder instead of concatenation.

C
C
char result[1000] = "";
for (int i = 0; i < 100; i++) {
    strcat(result, "item");
}
JV
Java
// Inefficient - creates many String objects
String result = "";
for (int i = 0; i < 100; i++) {
    result += "item"; // bad!
}

// Efficient - single buffer
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
    sb.append("item");
}
String result = sb.toString();
Rule of Thumb
Use StringBuilder in loops. For simple concatenation, + is fine.

Common Mistakes

When coming from C, developers often make these mistakes:

  • String is a class with methods
  • Strings are immutable - operations create new strings
  • Use .equals() for comparison, not ==
Common Pitfall
Don't assume Java works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • String is a class with many methods
  • Strings are immutable
  • Use .equals() not == for comparison
  • Use StringBuilder for efficient building
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Java to practice these concepts.
PreviousNext