Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Additional Binary Operators

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

I have a question regarding Casting Primitive Values.

In the book OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 >
Chapter 2: Operators and Statements > Using Additional Binary Operators:

I got this: long t = 192301398193810323;  // DOES NOT COMPILE

It says it does not compile.
But in the editor when I tried it compiled.

Please suggest.

Here is the online book URL:
http://techbus.safaribooksonline.com/book/certification/oracle/9781118957424/chapter-2-operators-and-statements/c02_level1_2_html

Thanks,
Prabal.
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Prabal Nandy wrote:But in the editor when I tried it compiled. 
Are you sure?

Since the literal 192301398193810323 doesn't have l Or L added at the end of It, by default It is considered as an int literal but that value doesn't fit in the range of int type i.e. -2147483648 to 2147483647 ergo It will not compile.

To make It compile successfully you have to append l Or L at the end of the literal like this 192301398193810323l Or 192301398193810323L then Java will consider It as long literal where that value fits in the range of long type i.e. -9223372036854775808 to 9223372036854775807. Capital L is recommended over l.

If still has problem you can show us the code.
 
Prabal Nandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick response.
It is compiling successfully.
However I am getting error at run-time.
Error(12,22):  integer number too large: 1923013981938103232.

I think here the fact is: By default the value is considered as an int literal and we can assign an int into a long literal (since long type has a higher range than a int type, the int will be converted to long at compile time). However at run-time it will check the range and show the error.

Please advice!
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabal,
The book is correct. It doesn't compile.
Remember that you should always use javac from the command line to compile your code when you are studying for the exam. IDEs do a lot more than just pure compilation and so they obscure a lot of fundamental things, which may lead you to believe something that is fundamentally incorrect. (This question of yours is a very good example!)

There are situations where IDEs can even execute code and produce output even when the code has compilation errors. So again, always rely on command line javac and java tools to compile and execute your programs.

HTH,
Paul.
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do one thing, write the following code in a Notepad Or Notepad++ and compile using javac command on command prompt and let us know what happens.
 
Prabal Nandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Paul!
You are right. However after compiling the line I was not able to execute it. Not sure why.

D:\src>javac Sort.java

D:\src>java Sort
Error: Could not find or load main class Sort


Code block:
-----------

public class Sort
{
public static void main( String[] args )
{
    long t = 1923013981938103232L;  // DOES NOT COMPILE
}
}

Thanks,
Prabal.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you are expecting something to run when it doesn't even compile. You get the class file after compilation. If the compilation fails, the class file won't be created, so how will it run??

BTW, the original code in the book did not have L at the end of the number. Your code has. So your code will compile and produce a class file. Run the dir command to see the contents of the directory after compilation.
 
Prabal Nandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiled and class file was generated.
However while running it showed the error.
Error: Could not find or load main class Sort

I set the classpath also but still showing the same error.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running this:
java -classpath . Sort
If it still doesn't run, post a screen shot of the command line showing the output of javac, dir, and java
 
Prabal Nandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul,
It worked.
Thanks.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Paul's solution worked then that implies you have a CLASSPATH set in your environment variables.
 
Prabal Nandy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have CLASSPATH set in my environment variables.
Basically this command worked for me:
D:\src\view>java -cp .. view.Sort

Since my java file was under view package I needed to specify the fully qualified class name (view.Sort) and since my java and class file location was same (under view folder) I needed to use ".." as the class path to go back to src folder to mention fully qualified class name.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic