• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compilation error while assign int to Long variable

 
Greenhorn
Posts: 10
1
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote code:



For last line code I receive compile error: "Incompatible types: int cannot be converted to java.lang.Long".

Why I didn't receive this error message with statement "long longVar = 10;"

I know for fixing I just need add letter L like this: "Long longInst = 10L;" and compilation will be success.
But I don't understand why this statement work "long longVar = 10;" and this statement don't: "Long longInst = 10;".
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dmytro Grytskiv wrote:
Why I didn't receive this error message with statement "long longVar = 10;"

I know for fixing I just need add letter L like this: "Long longInst = 10L;" and compilation will be success.
But I don't understand why this work "long longVar = 10;" and this not: "Long longInst = 10;".



There are two automatic conversions going on here. First is implicit casting. And second, is autoboxing. The automatic conversion (for assignment) is specified by by Section 5.2 of the Java Language Specification ... see https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

And notice that implicit casting, followed by automatic boxing is *not* one of the automatic options.

Henry
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to what Henry posted,

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

This means that you no longer have to write it as :
Integer in = new Integer(10);
or
Character ch = new Character('c');

Instead, you use a shorter code to do the same as
Integer integer = 10;
Character ch = 'c';

Secondly, the implicit cast allows you to set a variable with value thats allowed to fit into it since widening is allowed and narrowing is not (without typecast)
Note that the following code would still work:

Long longValue = (long) 10;
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to Coderanch
 
Dmytro Grytskiv
Greenhorn
Posts: 10
1
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks. Now I see this.

Uncompilable code: "Long longInst = 10;" can be replaced with this compilable code:

- there no more two conversations within one operation.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Long longValue = (long) 10;


Or just Long longValue = 10L;
Never use a lowercase L though, it's too hard to distinguish from a 1 in some fonts.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dmytro
Note one thing, when you initialize variables, your literal value types are either int or double (unless you explicitly specify one), no matter what variable data type is.

None of those two lines below would compile.

  • First line wouldn't compile, even though long can hold it, but 4222222222 is too big to be as an int.
  • Second line, fractional number always is a double type, so Java asks your acknowledgment if you're aware of that and requires either cast or specify type explicitly.

  • Wouldn't compile: Fixed:

    And welcome once again
     
    It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic