• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

mock exam questions

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen a code snippet in a mock exam:

SomeClass firstArray[];
Object myArray[] = {firstArray};

what does {} do?

Also is String="" the same as String=null?

In
byte b = 1;
++b;

Does ++b promote byte b to an int?
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!!

what does {} do?

You can initiliaze an array in two modes:

1) give a finite dimension
int[] array2 = new int[2];

2) give knowned values.
int[] array1 = new int[]{1,2};

{} initializes an array.
[ August 15, 2007: Message edited by: Collins Mbianda ]
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"" is the empty String and it's different from null
String empty = "";
String empty2 = null;

The method length() will return 0 on empty
and will throw a NullPointerExcetion on empty2.
[ August 15, 2007: Message edited by: Collins Mbianda ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by krishna vijay:
Also is String="" the same as String=null?


I guess you mean something like String s = "" vs. String s = null. No, these are not the same. In the first case, you let s refer to a String object of length 0, and in the second case you let s refer to no object (null).
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please can anyone explain the
third question put up by
krishna

ie


In
byte b = 1;
++b;

Does ++b promote byte b to an int?



i have one more doubt about the initialisation of array actually
what value gets assigned in the first question
to myArray[],since firstArray is also an array?

SomeClass firstArray[];
Object myArray[] = {firstArray};


put up
by krishna................


if anyone can explain it will be kinda of you.....

Thanks in advance..............
[ August 16, 2007: Message edited by: dhwani mathur ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

dhwani mathur posted Today 13:15


i have one more doubt about the initialisation of array actually
what value gets assigned in the first question
to myArray[],since firstArray is also an array?


You are right, but an array is also an object. So the object-array contains only one element, the array of SomeClass-objects.
So when you have:
Object myArray_1[] = firstArray;

then the variable now just refers (points to) the first Array.

However, when you have
Object myArray_2[] = { firstArray };
a new array of objects is created, the only entry being the object array.


Another example with some output:

Prints:
Length of array: 2
Content:
Element 0: 12
Element 1: 17
---------------
Length of array: 1
Content:
Element 0: [Ljava.lang.Integer;@82ba41
---------------




Yours,
Bu.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a Lot!!! Mr Burkhard Hassel
for such a gud explanation of array,now
i got my doubt cleared
but still one doubt reamins as
below



please can anyone explain the
third question put up by
krishna

ie


In
byte b = 1;
++b;

Does ++b promote byte b to an int?



Thanks in advance................
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy all,

Dhwani again:

Does ++b promote byte b to an int?



No.

You can use this code to test, if it would promote b to an int, the code should not compile:

If int line two, the byte would be changed into an int, the code could not compile, because there is no int-version of the method, and an int wouldn't fit. Test it by outcommenting the third line and what the compiler thinks about it.
But it compiles as it is. Please don't be surprised about the output...
That would be a different question.

&: Don't mix this ++ up with +=

Because the += operators do promote conversion:

Without the byte to int conversion in this compound operators (+=, *= ...) you would have to cast manually but it doesn't.
The output will be the lotto numbers of next week.


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

Originally posted by dhwani mathur:
Does ++b promote byte b to an int?

Have a look at 15.15.1 Prefix Increment Operator ++ in the Java Language Specification
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dhwani mathur:

please can anyone explain the
third question put up by
krishna

ie


In
byte b = 1;
++b;

Does ++b promote byte b to an int?



Thanks in advance................ [/QB]



The simple rule to remember is that :

Default result type of any arithmetic operation is of type int
So althought j=j+1 is same j++ ,the result would be int in first case but byte in second one.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!!
for such a gud explanation....
Mr Burkhard Hassel
Manfred Clug
Vikrant.............

Now i got my doubt cleared!!!




Preparing SCJP 1.5
 
Do Re Mi Fa So La Tiny Ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic