Jump to content

Search the Community

Showing results for tags 'strings'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • New Member at General Forum?
    • Welcome Lounge
    • General Forum Announcements
    • Suggestions and Feedback
  • The General Forums
    • General Discussions
    • News Hub
    • Gamers Den
    • Sports & Entertainment
    • Techno Geeks Unite
    • The Marketplace

Blogs

There are no results to display.

Calendars

  • Community Calendar
  • Default Calendar

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


About Me


Location


Gender


Interests


Occupation


Xbox Live Gamertag

Found 1 result

  1. So I have been looking into different ways to optimize and tune my code, when I ran into an article about strings. Now there are two types of objects in java, mutable objects, and immutable object; rather, changeable and unchangeable objects. Unfortunately, a String is among those immutable objects. It cannot change once it has been created. Now you say, "Well, how is that possible? It's easy to set a String to a new value!" Of course that it is easy, but that is because the String class handles all the low level operations to 'modify' the String. For example, let's say we have a simple class that takes a name input import java.io.Serializable; import java.util.Scanner; public class RegisterUser implements Serializable { private static transient Scanner iReader; private static transient boolean nameOK = false; private static String username = null; public static void main (String args[]) { println("Hello, Welcome to the new user registration."); println("Fortunately, all we need at this point is your name."); iReader = new Scanner(System.in); while (!nameOK) { println("If you please, what is your first name?"); username = formatCase(iReader.nextLine()); println("Is " +username+ " correct? if it is, please enter Y"); if (scanner.nextLine().toString().equalsIgnoreCase("y")) nameOK = true; } nameOK = false; while (!nameOK) { println("Thank you. Now, what is your last name?"); String lastName = formatCase(iReader.nextLine()); println("Is " +lastName+ " spelled right? if it is, please enter Y"); if (scanner.nextLine().toString().equalsIgnoreCase("y")) { nameOK = true; username += " " + lastName; } } println("Thank you very much, " +username+ ", your username has successfully been registered."); } static String formatCase(String s) { return (s.length()>0) ? Character.toUpperCase(s.charAt(0))+s.substring(1) : s; } static void println(String s) { System.out.println(s); } } This class, from our point of view, is simple, and gets the job done quick. However, if we were to modify this to serve a large number of users concurrently, we'd run into a problem. When we do things, such as modify a string, we are not actually modifying it. We are creating two objects to replace it. When we used the line:
×
×
  • Create New...