Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Dan's Q - char to byte

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class JSC201 {
static byte m1() {
final char c = '\u0001';
return c; // 1
}
static byte m3(final char c) {
c = 's'; //I added this line
return c;} // 2
public static void main(String[] args) {
char c = '\u0003';
System.out.print(""+m1()+m3(c));
}
}
I dont understand why line 2 is a compiler error. The method m3() is returning c, which is a compile time constant, since its declared as final,
AND I am assigning a value to it.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cathy.
A simple name is a compile-time constant if (1) it is declared final and (2) it has a variable initializer and (3) that initalizer is a compile-time constant.
final int x1 = 10;
final int x2 = x1;
final int x3 = x1 + x2;
final String s1 = "abc";
final String s2 = s1;
A final parameter has a value at run-time - the argument of the method invocation.
final int y1; // no
final int y2 = y1; // no
final Object o1 = null; // no
final Object o2 = new Object(); // no
final String s3 = new String("abc"); // no
[ October 16, 2003: Message edited by: Marlene Miller ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marlene,
I understood your explaination, except for final String s3 = new String("abc"). Why can't we call it as compile-time constant?
Thanks..
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS says this:
15.28 Constant Expression
ConstantExpression:
Expression
A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:
* Literals of primitive type and literals of type String
* Casts to primitive types and casts to type String
...
i.e. Objects cannot be constant expressions, and new String("abc") is an Object (and not merely a string literal).
It seems to me that a good way of thinking about strings is somewhere between primitives and Objects, String objects seem to me to be a sort of wrapper around a string literal, much like Integer is a wrapper around int, but with automatic wrapping and unwrapping enabled (much as the Integer and other wrapper classes will behave in J2SDK1.5).
Of course the technical details may be much different to this, but I find it a good way to understand what is going on in these cases.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marlene and Anthony.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ArchanaG Reddy:
Hi Marlene,
I understood your explaination, except for final String s3 = new String("abc"). Why can't we call it as compile-time constant?
Thanks..


The String referenced by s3 is not created until run-time. Since it does not exist at compile-time, it can not be a compile-time constant.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String objects seem to me to be a sort of wrapper around a string literal,

I think it would be safer to think of a string as a wrapper around a char array. Take a peek at java.lang.String.

Now a char array is also an object, they say. This object has unnamed variables of type char, they say. So here is my abstract image of these ideas (It looks better in Visio.)

And I know it is hard to get used to, but the JLS says a string literal is a reference to an instance of class String.
That is, the source code �abc� represents a (32-bit or whatever) number that identifies some object on the heap. At some point the virtual machine replaces the source code string �abc� with a real reference.
Every now and then these details help me to analyze some tricky problems.
[ October 17, 2003: Message edited by: Marlene Miller ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to this forum and i would be thankful if you could tell me what Dans'Q....is it a mock exam or ??....give me the link .....
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vamsi
Welcome to the Ranch. Here's the link to Dan's website:
http://www.danchisholm.net
Good luck !
Regards
Harwinder
 
I've been selected to go to the moon! All thanks to this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic