aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
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();
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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

Joined: Mar 02, 2005
Posts: 580
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

Joined: Sep 26, 2005
Posts: 39
Thank you Vidya Sagar. Now I am clear.


Sampath
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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

Joined: Mar 02, 2005
Posts: 580

can we declare the overridsen method as 'final'


yes we can define override method as final
tian haitao
Greenhorn

Joined: Dec 05, 2004
Posts: 4
maybe the following text will help you.

http://forum.java.sun.com/thread.jspa?threadID=583406&messageID=2981209


trying and trying.
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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

Joined: Sep 28, 2004
Posts: 1228
Yes. But try to put it in finally and check the output.
tian haitao
Greenhorn

Joined: Dec 05, 2004
Posts: 4
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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: Sep 26, 2005
Posts: 39
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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: Sep 26, 2005
Posts: 39
then what if given as below.

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

Sampath
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
This is just a class with a static method ( main method )
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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
Jyothi Bhogadi
Ranch Hand

Joined: Jul 08, 2005
Posts: 47
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.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
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.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
why because, I can keep this page saved and can review before going to exam.

Thanks
Sampath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer