Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

What "type" is the command line args

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't this work?
public class testing
{

public static void main( String args[] )
{
String stringInput = args[0];

if( stringInput == "thisone")
{
System.out.print(stringInput);
}
}
}
it compiles ok
but when run like this:
java testing thisone
it doesn't give the expected output. It never enters the if statement. If I output the value of thisone to the screen it displays properly.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this should be your if statement to make a comparison:
if( stringInput.equals("thisone"))
I don't remember the explanation for why yours doesn't work. I'm sure someone else knows and will reply.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi-
In java Strings are objects. Unlike the other objects in java Strings have a special characteristic. When you declare a String object like this:
String s = "This Sting";
you are actually creating what is called an anonymous object. This means that as long as a String object is declared as a literal, the == operator will work.
Remember:
this == that; is true if and only if the the two objects are the same exact object. The equals method in the String class (and most classes) compares the values stored in the String object. Thus the following:
String ob1 = "Hi";
String ob2 = new String("Hi");
String ob3;
String ob4 = "Hi";
ob1 == ob2;//false since new String("HI") creates another object
//with the stored value of "Hi"
ob1.equals(ob2);//true because the value is the equal
ob3 = ob1;
ob1 == ob3;//true because they represent the exact same object
ob1 == ob4;//true because both String objects are represented in
//the one anonymous class for Strings
Kyle
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
Rhe is correct, the reason your code does not give the expected result is because == will compare the contents of the stack memory for the two referances, this is fine for comparing two primitive types as the actual values are stored there.
For objects (such as strings) only the address of the object ids stored on the stack, so == when used with strings will see if the two referances are pointing at the same string object (ie the same address), So we use the equals method (which is inherited from object and overridden by the String class) to compare the contents of the actual object.
Jane
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name, not obviously fictional.
Thanks,
Cindy
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of condition with String is not recommended. Or if in case we would want to do it, we forcefully use predefined methods for it.
------------------
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic