• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Three doubts not exactly explained in OCA exam book

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would be grateful if someone could explain these three doubts connected with the OCA exam.

1) Literals vs. FLOAT, DOUBLE, LONG
Somewhere I found info that 'float' needs 'f' after literal. That 'long' needs 'l' after literal. That in case of 'double' it is optional to add 'd'.
But is there any difference in concept when we consider primitive vs. wrapper class? I ask because during going through practice exams sometimes I saw, for example, 'long'/'Long' (don't remember) without additional 'l'/'L' at the end of literal. Maybe there is even another cause of such situation? Maybe auto-boxing?

2) static Integer or static int - When it needs to be initalized?
I know that when we have static variable in interface, it must be initialized in declaration line / in initialization code block / in constructor.
If we have a class variable (not instance, not local) as far as I remember, it is also necessary to initialize it early, exactly in declaration line (I think).
Then what if we have an instance variable with 'static' and it is not initialized in declaration line? When we have to initialize it? Does it change anything if we consider primitve type vs. wrapper class?

3) import static
I have some problem here. I thought that using wildcard in the case of import static is not allowed (that you have to indicate exactly the method or the variable). But yesterday I saw an example where there was a '*' (wildcard) symbol involved.

Thank you in advance for your time.
Best regards
Mike
 
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Michal Gadomski wrote:. . . 1) Literals vs. FLOAT, DOUBLE, LONG

Do you mean Long, long, or LONG? You need to be very careful about spellings when you are programming.

Somewhere I found info that 'float' needs 'f' after literal. That 'long' needs 'l' after literal.

That is correct, but never use “l”. Always use “L” for reasons explained here in the Java┬« Language Specification (=JLS).
Only use floats if some other API forces you to; I don't think they are suitable for use in normal programming, though they sometimes appear in exams.

That in case of 'double' it is optional to add 'd'.

If you write 123.45 or 123.45e-67 or similar with a decimal point, that format defaults to a double literal and there is no need for the “d”. If however you want a whole number double literal, you have the option of writing 123.0 or 123d. That is how the “d” is usually used

But is there any difference in concept when we consider primitive vs. wrapper class? . . . I saw, for example, 'long'/'Long' (don't remember) without additional 'l'/'L' at the end of literal. Maybe there is even another cause of such situation? Maybe auto-boxing?

Yes, wrapper objects and primitives are implicitly different. Look at this code.If you try line 5, you will find you cannot combine an implicit cast and boxing, so it won't compile.

2) static Integer or static int - When it needs to be initalized?
I know that when we have static variable in interface, it must be initialized in declaration line / in initialization code block / in constructor. . . .

Beware of static fields; many people consider that a static variable constitutes a design error.
Initialise static fields where they are declared or in a static initialiser; if you initialise them in the constructor, their value will change depending on how many instances have been created. So don't initialise them in a constructor or instance initialiser.
Interfaces don't have static variables; their fields are implicitly constants. JLS link (Java9 version).

Does it change anything if we consider primitve type vs. wrapper class?

A primitive can constitute part of a compile‑time constant (e.g. 123; a wrapper object (e.g. Integer.valueOf(123)) is never a compile‑time constant. Otherwise no noticeable difference.

3) import static . . . there was a '*' (wildcard) symbol involved.  . . .

The * symbol is permitted in all kinds of import declaration, but it is better to avoid it if possible. JLS section; see part 7.5.4.
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . . JLS link (Java9 version).

Sorry for my mistake; that should have read Java8. I can see no change in the Java12 version of that section.
 
Michal Gadomski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answer . About table with long and Long -> how such table would look like considering float and Float? Just a sec ago I saw a task connected with float. One of answers looked like this:

final float FEE = 0.012;

And it was said that such notation is not legal. That it needs 'f' at the end. If we look at your table with long/Long, we can see that (according to that logic) in case of this float, we should see implicit (float) cast. But not. Is it some exception? It looks quite difficult.
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michal Gadomski wrote:Thank you for your answer

That's a pleasure

. . . .

As you can see from this JLS section, that code won't compile without the “f” or an explicit (float) cast. If you look in this JLS part, you will find that is a narrowing conversion and desn't have an implicit cast.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!