Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

compare

Posted on 2004-04-28
10
Medium Priority
?
353 Views
Last Modified: 2011-09-20
hi expert..
say:
int i = 500;
int j = 600;

String s1 = "Sad";
String s1 = "Bad";

if( i == j ) or if( s1.equals( s2) ) more faster???

please explan why?
0
Comment
Question by:Dishan Fernando
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 10937801
int will be faster - as it's a leaner operation. Why are you interested?
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 300 total points
ID: 10937806
primitive data type will be faster always.

i == j is a simple computation....

String is an object and there is a method invocation will cost bit more time

So always the primitive data type operations are faster than the object comparison.


Regards,
Muruga
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937815
Hang on - they're actually likely to be virtually identical
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:searlas
searlas earned 100 total points
ID: 10937846
You're comparing an operator to a method invocation.  It doesn't matter that you're calling 'equals' on the string, it could be any method.

I'll go out on a limb here and say it's highly likely that ALL operators in Java are faster than method invocations.

i.e.  i == j, i << j, i + j, i * j, i / j.....

Faster than.
obj.method(param);

Method invocation implies stack manipulation, looking up method entry point etc etc...
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 300 total points
ID: 10937852
I am strongly saying that primitive data types are much faster than anything.  
Object are always slower than the primitive data type operations.


Here is the proof...

      public static void main(String[] args)
      {
            String a = "hello";
            String b = "whatis";
            
            int aa = 500;
            int bb = 600;
            
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++)
            {
                  if (aa == bb)
                  {
                  }
            }
            System.out.println("Integer comparison... time taken: " + (System.currentTimeMillis() - start));
            
            start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++)
            {
                  if (a.equals(b))
                  {
                  }
            }
            System.out.println("String comparison... time taken: " + (System.currentTimeMillis() - start));
      }
0
 
LVL 7

Expert Comment

by:searlas
ID: 10937861
(before someone points it out... I'm not including str1 + obj as an operator as that implies calls to toString... therefore has implicit method invocation...)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937874
I was thinking that the VM might have a way of knowing that a String taken from the constant pool is not the same as another one taken from the constant pool, but on inspection, it looks like this is not the case. The only real way to ascertain this sort of thing is by profiling it. 'Opinions' are not very useful
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937886
>> String b = "whatis";

String b = "whati";

with same length data with different string will increase the time taken...
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937901
that's great :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937940
Along the lines of what i was getting at

         String a = "hello";
         String b = "hello";

will take quite a bit less time and

            String a = "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            String b = "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

won't be any slower
         
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question