Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparison of data types  RSS feed

 
nilesh kamani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

can anyone please tell me which comparison of two same data type is efficient?
short == short ?
byte = byte?
int == int ?
float == float?
String = String?


I am using comparison at many places in my program to decide the flow of program.


Thanks & Regards
Nilesh Kamani
 
vinay vishal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comparison of int, short, boolean is ok but when you use float or double it will never make true due to its precesion.
Example:-
int a = 15;
int b = 4;
float rslt = a/b;
and now when you solve 15/4 you get "rslt = 3.75"
but when you will go to make comparison like this
if(rslt == 3.75){
// some logic here
}
Statement inside if never execute because expresion will always return false.

Example for String:-
String str_const1 = "abc";
String str_const2 = "abc";
String str_obj1 = new String("Hello");
String str_obj2 = new String("Hello");
Now,
str_const1 == str_const2 // return true
str_obj1 == str_obj2 // return false;
str_obj1.equals(str_obj2) // return true;
str_obj1 == str_obj2 // return false (different object, if you want to know in detail read "Head First java")

I think you got it.
Bye
 
nilesh kamani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which is fastest comparison?
byte == byte?
int == int?
short == short?
String == String?


Regards
NIlesh Kamani
 
Vipin Thomas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte comparison should be the fastest as it takes least memory amongst all.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte comparison should be the fastest as it takes least memory amongst all.


Alas this is incorrect because all byte and short are converted to int for all arithmetic operations. Its in the language spec.

Bill
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nilesh, I think your program would have to be very unusual in order for the performance differences in these comparisons to make a difference. This sounds a lot like premature optimization, to me. And the decision of which datatype to use should probably be made based on the nature of the data, not on which comparision is believed to be faster than another.
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String == String is probably something you will rarely need. Like the other comparisions it compares the value, but since String in an object, the value is a memory address, not the actual String.

What you likely want is

str.compareTo(otherStr)

or

str.equals(otherStr)

or

str.equalsIgnoreCase(otherStr)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!