• 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

StringBuffer

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why the following code is compile time error as

incompatible types
found : java.lang.String
required: java.lang.StringBuffer
StringBuffer s[]={"A","B"};


class Q2
{
public static void main(String arg[])
{
StringBuffer s[]={"A","B"};
System.out.println(s[0]+","+s[1]);
}
}

please clarify me

Thanks
Sampath
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why the following code is compile time error as



hi sampath

StringBuffer cannot be created as String is created

For eg:
String s = "a";
----> its possible

StringBuffer s ="a";
----> its not possible

Stringbuffer can be created only by
StringBuffer ss = new StringBuffer();
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sampath,
When you read the error it clearly states incompactable types.
StringBuffer s[]={"A","B"};

What happens here is a an array of String buffer is created and in the same line , each element in the array is as assigned with a String value.
So here a string is assigned to a String buffer and hence the error

You can modify the code like

StringBuffer s[]={new StringBuffer("A")};
[ September 30, 2005: Message edited by: Srinivasa Raghavan ]
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it. thank you very much.

one more doubt:

char c = '\u000d';
char c1 = '\u000a';
char c2 = '\u0000';
char c3 = '\uface' ;
char c4 = '\u00001' ;
char c5 = '\101';
char c6 = 65;
char c7 = '\1001' ;

from the above declarations why c,c1 and c7 are invalid whereas c3 is valid.

Please clarify me.

Thank you
Sampath
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c = '\u000d';
char c1 = '\u000a';

throws error because of newline charater

char c7 = '\1001' ;

throws error because u is missing

char c3 = '\uface' ;

works fine because it is hexadecimal representation of a number
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vidya Sagar. Now I am clear.


Sampath
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below code can we declare the overridsen method as 'final', whereas it is not declared as 'final' in the super class.

class A
{
public void callme()
{
System.out.println("A");
}
}
class B extends A
{
public final void callme(){}
void callMe(){}
void callme(int a){}
void callme(){}

}

Please clarify me

Sampath
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


can we declare the overridsen method as 'final'



yes we can define override method as final
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe the following text will help you.

http://forum.java.sun.com/thread.jspa?threadID=583406&messageID=2981209
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IN the below code why the commented line //1 won't print. Is it because of return; statement in the try block.


public class exception
{
public static void main(String args[])
{
System.out.println("A");
try
{
return;
}
catch(Exception e)
{
System.out.println("B");
}
System.out.println("C");//1
}
}


Thank You
Sampath
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. But try to put it in finally and check the output.
 
tian haitao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
of course,it won't reach the commented line //1 unless the exception appeared in the try block.

i think so.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will reach.
finally block will run even if there is no exception occurs at runtime.

Also finally will not run only if System crashes or if you give System.exit(0).
[ September 30, 2005: Message edited by: Srinivasa Raghavan ]
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we put it in finally it will print.
Thank You.

one more question.

How come the output from the below program is 0.

class Q30
{
static int p=abc();
static public int abc()
{
int i=123;
System.out.println(p);
return i;
}
public static void main(String arg[])
{
}
}

Please explain

Thank You
Sampath
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables gets initilized when the class is loaded and hence the output.
I feel you can start new thread ...

When this line int p=abc(); is executed it calls abc(), there it printd the default value of p.

[ September 30, 2005: Message edited by: Srinivasa Raghavan ]
[ September 30, 2005: Message edited by: Srinivasa Raghavan ]
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what if given as below.

class Q30 {
public static void main(String arg[]) {
}
}

Sampath
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a class with a static method ( main method )
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot declare static variables in an inner class.

but how can we declare a variable as 'static final' in an inner class. Please explain

Ex: static int i=9; // not acceptable
static final int j=8; // acceptable


Thank You
Sampath
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sampath,
didn't really get you what you were trying to ask from your previous question.
If that was your question, then nothing would be the output as there is nothing much to be done in that program.

Regards
Jyothi.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5 Quesions answered in just one thread...Wow...
sampath you could have started different threads for different questions...
I dont think its a good practice to keep on asking different unrelated questions in one thread.
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why because, I can keep this page saved and can review before going to exam.

Thanks
Sampath
reply
    Bookmark Topic Watch Topic
  • New Topic