• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

use of quotes in type-in qts

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1.
If we are asked to type in the value of 5 in hex format . which one of the following should be typed in ?
0x05
0x0005
'0x05'
Q2. While typing in string values (for type-in questions) should we use quotes ?
eg
String str = "hello" + "world"
are we supposed to use quotes ?
Ans : "helloworld"
???
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No quotes are needed.
As for 0x05 vs. 0x0005, both are valid - more info is needed. 0x05 is a byte value; 0x0005 is a short or char.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Quotes are needed for this to compile and print "helloworld" , if hello & world are not already defined as strings as follows..
String hello = "hello";
String world = "world";
String str = hello + world;
System.out.println (str);
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
As for 0x05 vs. 0x0005, both are valid - more info is needed. 0x05 is a byte value; 0x0005 is a short or char.


Could u pls give me more inputs on this :
how does 0x05 translate to a byte value ?
and how is 0x0005 a short or char ??
I thought both are intergal literals. And integer literals are always of type int.
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer literals are always up to int type, this means the could be: byte, short, char, int.
In hexadecimal notation a digit represents 4 bits, so 0x05 is a byte and 0x0005 is an short, because 2x4=8(byte) and 4x4=16(short).
What�s the high value that you can represent in 4 bits?
1111 = 15 that in hexadecimal is F
So for each hexadecimal value you have 4 bits:
Given the following pattern: 0001 0010 0011 0100 the hexadecimal representation would be: 0x1234
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 3.10.1 :
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8):
An integer literal is of type long if it is suffixed with an
ASCII letter L or l (ell); otherwise it is of type int (�4.2.1).
Therefore, 0x05 = 0x0005 = int type ! as per JLS !
The JLS never says its of type byte or short. Could u tell me where did u get this info from ?
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be something wrong, because if you follow the link to 4.2.1, it brings up:
Integral Types and Values
The values of the integral types are integers in the following ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807, inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marcela Blei:
There must be something wrong, because if you follow the link to 4.2.1, it brings up:


Yes it does tell u the integral types. But the JLS clearly says that integer literals are of type int.
It does not say its of integral type !!!
RHE too does not mention that 0x05 is a byte !
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marcela Blei:
There must be something wrong, because if you follow the link to 4.2.1, it brings up:


Yes it does tell u the integral types. But the JLS clearly says that integer literals are of type int.
It does not say its of integral type !!!
RHE too does not mention that 0x05 is a byte !
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read on... the JLS in 5.2 allows a narrowing conversion assignment without a cast if
1)The expression is a constant expression of type int.
2)The type of the variable is byte, short, or char.
3)The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
integer literals are constants so.....
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point here is NOT Assignment conversion.
The question is :
1. What are u supposed to type if we are asked to write the hex value of 5 ? 0x5 or 0x05 (JPrep specifies this) or 0x0005 ?
2. The type of integer literal is int. and not byte or short or char.
Anyway, FYI, you are referring to the old JLS , heres the new version but its irrelavant to my question though
In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
The expression is a constant expression of type byte, short, char or int.
The type of the variable is byte, short, or char.
The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, forget I said anything about byte values or short values. I forgot that all integral literals are int by default. So really there's no single way to answer the question - 0x05 and 0x0005 are equally valid. So are 0x5 and 0x000000000000000000005. If you're asked this question on a real exam, you just have to hope that the authors aren't complete idiots, and that they will accept any of the above as the correct answer. If that doesn't work, then tell me and I will drive over and throw rocks at Sun's offices and yell at them for asking an unanswerable question on the exam. But I don't think that will be necessary, because if there were such a poor question on the actual exam we would have heard more complaints about it by now.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if they ask you to type in how a value will be represented in hexadecimal, the question will say exactly how many characters you should use to represent that value.
for example , represent 4 in hexadecimal using only 4 characters.
So the answer is -------- 0x04
I dont know whether they have any variation on this.
 
Deepak M
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx V !
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am still not following this hex format.
How do you write int 5 in hex format . Is it 0x5?
I am confused now. Please someone help me.
Thank you
VR
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any of these are valid: 0x5, 0x05, 0x0005, or however many zeros you want. It's impossible to pick one unless there is more info given in the problem. Don't worry about it.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic