• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

String concatenation

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there;)

I am currently working my way through different Practice Questions for the OCA Exam.

I recently got this question:

Given:



Then it was asked how many objects have been created here?

The right answer was 4, but I am not quite sure why.

Am I right, that the first String-Object is created here:   String s = "Hello"; //the Object is "Hello"

Then, the second String-Object is created here:   s.concat(" World");         //the Object is " World"

Also, the third String-Object ist created here:   s.concat(" World");            //the Object is "Hello World"

At last, the fourth String-Object is created here:   s.toLowerCase();           //the Object is "hello world"

The System.out.println(s) only prints out "Hello" as String is immutable, right?

Kind regards
Florian
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
At last, the fourth String-Object is created here:   s.toLowerCase();           //the Object is "hello world"



The String Object is actually "hello" created from this line, but the rest are correct.

Henry
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks;)
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!