Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Long.parseLong(String s) and new Long(String s)?

I know that the String can be converted to a long using Long.parseLong(String) method and Long(String) constructor.

String str="12356";
Long myvar= Long.parseLong(str);
Long myvar2 = new Long(str);

Both of them gives same output. Value of myvar and myvar2 is same. I would like to know which one gives better performance and when to use parseLong and when to use new Long(String s).

like image 727
Chaitanya Avatar asked Jan 02 '14 08:01

Chaitanya


People also ask

What does long parseLong do in Java?

The java. lang. Long. parseLong(String s) method parses the string argument s as a signed decimal long.

What does the method parseLong string do?

The parseLong() method of Java Long class is used to parse the given string argument as a signed long in the radix which is represented by the second argument.

What is the return type of the method parseLong string?

This method returns the long represented by the string argument in the specified radix.

How do you make a string longer?

There are many methods for converting a String to a Long data type in Java which are as follows: Using the parseLong() method of the Long class. Using valueOf() method of long class. Using constructor of Long class.


2 Answers

The difference is

  • parseLong returns a primitive
  • new Long() will always create a new objecct
like image 59
Prasad Kharkar Avatar answered Nov 08 '22 01:11

Prasad Kharkar


new Long will always create a new object, whereas parseLong doesn't.

I advise you to go through the implementation of each one.

like image 6
Maroun Avatar answered Nov 07 '22 23:11

Maroun