DEV Community

Sudhakar V
Sudhakar V

Posted on

DAY 6 JAVA- STRING FUNCTIONS & CONSTRUCTORS

Java Functions and String Class

Functions in Java

1. Method Basics

  • Definition: A block of code that performs a specific task
  • Syntax:
  modifier returnType methodName(parameters) {
      // method body
      return value; // if returnType is not void
  }
Enter fullscreen mode Exit fullscreen mode
  • Example:
  public int addNumbers(int a, int b) {
      return a + b;
  }
Enter fullscreen mode Exit fullscreen mode

2. Method Types

  • Static methods: Belong to the class (ClassName.methodName())
  • Instance methods: Belong to objects (object.methodName())
  • Built-in methods: Predefined in Java libraries (e.g., Math.sqrt())

3. Method Overloading

Multiple methods with same name but different parameters:

void print(int i) { System.out.println(i); }
void print(String s) { System.out.println(s); }
Enter fullscreen mode Exit fullscreen mode

String Class in Java

1. String Creation

String s1 = "Hello";              // String literal (stored in string pool)
String s2 = new String("Hello");   // String object (heap memory)
Enter fullscreen mode Exit fullscreen mode

2. Important String Methods

Comparison Methods

boolean equals(String str)        // Case-sensitive comparison
boolean equalsIgnoreCase(String)  // Case-insensitive
int compareTo(String)            // Lexicographical comparison (returns 0 if equal)
Enter fullscreen mode Exit fullscreen mode

Search Methods

int length()                     // Returns string length
char charAt(int index)           // Character at specific position
int indexOf(String str)          // First occurrence of substring
int lastIndexOf(String str)      // Last occurrence of substring
boolean contains(CharSequence)   // Checks if string contains substring
Enter fullscreen mode Exit fullscreen mode

Modification Methods

String concat(String str)        // Concatenation (same as + operator)
String substring(int begin)      // Extracts substring
String substring(int b, int e)   // Extracts substring from b to e-1
String replace(char old, char new) // Replaces characters
String toLowerCase()             // Converts to lowercase
String toUpperCase()             // Converts to uppercase
String trim()                    // Removes leading/trailing whitespace
Enter fullscreen mode Exit fullscreen mode

Conversion Methods

static String valueOf(primitive) // Converts primitive to String
char[] toCharArray()             // Converts to character array
String[] split(String regex)     // Splits string using regex
Enter fullscreen mode Exit fullscreen mode

3. String Immutability

  • Strings are immutable (cannot be changed after creation)
  • Operations like concat(), substring() return new String objects
  • Example:
  String s = "Hello";
  s.concat(" World");  // Returns new String, original unchanged
  System.out.println(s); // Still prints "Hello"
Enter fullscreen mode Exit fullscreen mode

4. StringBuilder (Mutable Alternative)

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");  // Modifies existing object
System.out.println(sb); // Prints "Hello World"
Enter fullscreen mode Exit fullscreen mode

5. Important Notes

  • Always use equals() for string comparison, not ==
  • For heavy string manipulation, use StringBuilder for better performance
  • String literals are stored in string pool for memory efficiency

Example Combining Functions and Strings

public class StringOperations {

    // Function to reverse a string
    public static String reverseString(String input) {
        return new StringBuilder(input).reverse().toString();
    }

    // Function to count vowels
    public static int countVowels(String str) {
        int count = 0;
        String lowerStr = str.toLowerCase();
        for (int i = 0; i < lowerStr.length(); i++) {
            char ch = lowerStr.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String text = "Hello World";

        System.out.println("Original: " + text);
        System.out.println("Reversed: " + reverseString(text));
        System.out.println("Vowel count: " + countVowels(text));
        System.out.println("Uppercase: " + text.toUpperCase());
        System.out.println("Contains 'World'? " + text.contains("World"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Common Errors in Java Constructors and How to Overcome Them

Constructors in Java are special methods used to initialize objects, but they can lead to several common errors. Here's how to identify and fix them:

1. Default Constructor Not Available When Needed

Error:

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }
}

// Later...
Person p = new Person(); // Compile error - no default constructor
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Either provide a default constructor:
  public Person() {
      this.name = "Unknown";
  }
Enter fullscreen mode Exit fullscreen mode
  • Or always use the parameterized constructor

2. Constructor Recursion (StackOverflowError)

Error:

public class Infinite {
    public Infinite() {
        new Infinite(); // Recursive call
    }
}
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Avoid calling the same constructor within itself
  • Use factory methods if complex initialization is needed

3. Field Shadowing

Error:

public class Rectangle {
    private int width;

    public Rectangle(int width) {
        width = width; // Assignment to parameter, not field
    }
}
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Use this keyword:
  public Rectangle(int width) {
      this.width = width;
  }
Enter fullscreen mode Exit fullscreen mode

4. Calling Overridable Methods in Constructor

Error:

public class Parent {
    public Parent() {
        init(); // Dangerous if init() is overridden
    }

    public void init() {
        System.out.println("Parent init");
    }
}

public class Child extends Parent {
    private String value;

    @Override
    public void init() {
        System.out.println(value.length()); // NullPointerException
    }
}
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Avoid calling overridable methods in constructors
  • Make methods final if they must be called:
  public final void init() { ... }
Enter fullscreen mode Exit fullscreen mode

5. Constructor Leaking 'this' Reference

Error:

public class Leaker {
    public static Leaker instance;

    public Leaker() {
        instance = this; // 'this' escapes before full construction
    }
}
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Avoid publishing this reference in constructor
  • Use factory pattern instead

6. Missing Super Constructor Call

Error:

public class Child extends Parent {
    public Child() {
        // Implicit super() called, but Parent has no default constructor
    }
}

public class Parent {
    public Parent(int x) { ... }
}
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Explicitly call parent constructor:
  public Child() {
      super(0); // Provide required parameter
  }
Enter fullscreen mode Exit fullscreen mode

7. Exception in Constructor

Error:

public class Resource {
    public Resource() throws IOException {
        // Might throw exception
    }
}

// Then creation fails:
Resource r = new Resource(); // Unhandled exception
Enter fullscreen mode Exit fullscreen mode

Solution:

  • Handle exception at creation point:
  try {
      Resource r = new Resource();
  } catch (IOException e) {
      // Handle error
  }
Enter fullscreen mode Exit fullscreen mode
  • Or use static factory method:
  public static Resource create() {
      try {
          return new Resource();
      } catch (IOException e) {
          return null; // or throw unchecked exception
      }
  }
Enter fullscreen mode Exit fullscreen mode

Best Practices for Constructors

  1. Keep constructors simple - delegate complex initialization to methods
  2. Use constructor chaining - one primary constructor with others calling it
  3. Consider static factory methods as alternative to complex constructors
  4. Make defensive copies when storing mutable parameters
  5. Document thread-safety if class is meant for multithreaded use

Example of Well-Designed Constructor

public class Employee {
    private final String name;
    private final int id;
    private final Date hireDate;

    // Primary constructor
    public Employee(String name, int id, Date hireDate) {
        this.name = Objects.requireNonNull(name, "Name cannot be null");
        this.id = id;
        this.hireDate = new Date(hireDate.getTime()); // Defensive copy
    }

    // Secondary constructor
    public Employee(String name, int id) {
        this(name, id, new Date()); // Default to current date
    }

    // Static factory method
    public static Employee createWithValidation(String name, int id) {
        if (id <= 0) throw new IllegalArgumentException("Invalid ID");
        return new Employee(name, id);
    }
}
Enter fullscreen mode Exit fullscreen mode

By following these guidelines, you can avoid most common constructor-related issues in Java. TBD

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay