Jump to content

Java - Mutable example

Travercraig

So i'm trying to show a simple example of using mutability in java. I managed to show immutability using a String which was easy because that is defininity immutable, but I'm not sure if using a type char varaible could demonstrate mutability.

 

public static void main(String[] args)
    {
        char phrase = 'a';
        
        System.out.println("Original: " + phrase);
        
        phrase++;
        
        System.out.println("\nModified: " + phrase);            
    }

 

My thought is that the variable 'phrase' is modified to increment, and when I print it out after increment it disaplays as:  "Modified: b"

 

Is this correct to showcase mutability?

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×