• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

output needs to be discussed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class StringDemon12
{
public static void main(String args[])
{
String a = "newspaper";
System.out.println(a);
a = a.substring(5,7);
System.out.println(a);
char b = a.charAt(1);
System.out.println(b);
a = a + b;
System.out.println(a);
}
}
OUTPUT:
newspaper
ap
p
app

Hi All,
with respective to the above program how for
char b = a.charAt(1);
System.out.println(b);
the output is p

can anybody provide the explanation for the above?

thanks
venkat
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the charAt index starts from 0, so charAt(1) returns 1.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class StringDemon12
{
public static void main(String args[])
{
String a = "newspaper";//1
System.out.println(a);
a = a.substring(5,7);//2
System.out.println(a);
char b = a.charAt(1);//3
System.out.println(b);
a = a + b;//4
System.out.println(a);
}
}

Hi Friend,
here at line 1 the a is refering "newspaper" but at line 2 the a is refering to "ap" now (because the output is assigned to a). the previous value "newspaper" is still in pool but without any reference.So at line 3 the b will get the value from the current value of a i.e. "ap" thats why its showing "p".
i suppose it will clear the doubt.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"venkatramsimha"

You have been asked over and over to change your display name to be your real first and last name. There are two seperate fields one for first name and one for last name, So you name will have a space between the first and alst names.

You have repeatedly ignored our requests. I will give you two days to change your display name or I will suspend your account.

Thanks

Mark
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic