0

This is a program which demonstrate the working of parseLong When i run this in bluej it gave me output as as given below

But when i ran this in console in ubuntu terminal by compiling using the command javac it gave me compilation error as given below

Only change i made to compile in javac was changing the line public void main() to public static void main(String args[])

when i ran in ubuntu terminal

it gave me error as

LongDemo.java:8: error: cannot find symbol
      long a = Long.parseLong("1452");
                   ^
  symbol:   method parseLong(String)
  location: class Long
LongDemo.java:9: error: cannot find symbol
      long b = Long.parseLong("26");
                   ^
  symbol:   method parseLong(String)
  location: class Long
LongDemo.java:10: error: cannot find symbol
      long c = Long.parseLong("54");
                   ^
  symbol:   method parseLong(String)
  location: class Long
3 errors

But when i ran ran using the prefix java.lang.Long.parseLong in ubuntu terminal

it worked fine

Why is it behaving like this in different scenarios

Here is the program

import java.lang.*;

public class LongDemo {

   public  void main() {

      // parses the string argument
      long a = Long.parseLong("1452");
      long b = Long.parseLong("26");
      long c = Long.parseLong("54");

      long m = a * b * c;
      System.out.print("Value after multiplying = " + m);
   }
}

My java version

midhun@midhun-Vostro-15-3568:~$ java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Output Value after multiplying = 2038608

Midhun Raj
  • 925
  • 2
  • 7
  • 21
  • Yes, you definitely shouldn't have to do that `import` – g00se Oct 26 '22 at 18:34
  • 1
    It smells as if there's some other Long class (not java.lang.Long) in scope. – access violation Oct 26 '22 at 18:40
  • 1
    Possibly .Did you make your own class called `Long`? – g00se Oct 26 '22 at 18:45
  • Please, can you post the code which does not compile and also the command you use to run it? – Christoph S. Oct 26 '22 at 18:50
  • @ChristophS. The same code with a difference in the line public void main() as public static void main(String args[]) showed the compilation error when i compiled in ubuntu from terminal using javac – Midhun Raj Oct 26 '22 at 19:11
  • @accessviolation ...you where absolutely correct ..when i made a new directory and when i ran this program in that directory it worked fine...Thanks man ...you rock – Midhun Raj Oct 26 '22 at 19:13
  • @accessviolation i was having another java program Long.java in the same folder,.,that was causing the trouble..thanks a lot man – Midhun Raj Oct 26 '22 at 19:15

0 Answers0