Difference between String=""; and String =null

I want to know the difference especially the ADVANTAGE between the following-
1. String s="";
String s="ABC"
and
String s;
String s="ABC";
or String s=null;
String s="ABC";
2. Object anyObject;
anyObject-new Object();
and
Object anyObject=null;
anyObject=new Object();

Tanvir007 wrote:
I want to know the difference especially the ADVANTAGE between the following-
1. String s="";s points to the empty string--the String object containing no characters.
String s="ABC"s points to the String object containing the characters "ABC"
String s; Declares String reference varialbe s. Doesn't not assign it any value. If it's a local variable, it won't have a value until you explicitly assign it. If it's a member variable, it will be given the value null when the enclosing object is created.
String s="ABC";Already covered above.
or String s=null;s does not point to any object.
String s="ABC";???
You've already asked this twice.
Are you talking about doing
String s = null;
String s = "ABC";right after each other?
You can't do that. You can only declare a variable once.
If you mean
String s = null;
s = "ABC";It just does what I described above, one after the other.
2. Object anyObject;
anyObject-new Object();
and
Object anyObject=null;
anyObject=new Object();As above: In the first case, its value is undefined until the new Object line if it's a local, or it's null if it's a member.
As for which is better, it depends what you're doing.

Similar Messages

  • Difference between .equals("") and .equals(null)

    Can any one please tell me the difference between the following?
    String someString;
    someString = someMethod(someParameter);
    After the method call, if I am going for a check like this..
    what will each check do?
    1. if (someThing.equals(null))
    2. if (someThing.equals(""))
    3. if (something == null )
    4. if (something == "" )
    Thanks in advance,
    Regards
    Rakesh

    First, understand that the empty string ("") and null are two very different things. The empty string ("") is a string object that just doesn't contain any characters. It's still an object though--you can call it's methods.
    Null, on the other hand, is essentailly nothing. It's a special value that means "Nothing. No object here." (Ignoring for now the distinction among the null type, a null reference, etc.) You can't call methods or refer to fields on null. If you try, you'll get a NullPointerException.
    1. if (someThing.equals(null))Calls the equals method on someThing to compare someThing to null. If someThing is not null, this returns false. If someThing is null it... what? returns true? Nope. Throws NullPointerException. You can't call the equals method on a null someThing.
    Therefore there's no point in ever using equals(null).
    2. if (someThing.equals(""))Calls the equals method on someThing to compare someThing to the empty string (""). If someThing is not null, but is empty, it returns true. If someThing is non-null and is not empty (e.g., "abc"), it returns false. If someThing is null, it throws NPE.
    One generally uses the equals method, not the == operator to compare objects--that is, to see if their contents or states are equal. The == operator is used to see if two referecnes refer to the same object, or if a reference is null.
    3. if (something == null ) Evaluates to true if something is null and to false if something is non-null. Never throws NPE (since we're not calling a method or accessing a field).
    4. if (something == "" ) You never really want to use == to compare a String variable to a String literal like this. This checks to see if the reference something is referring to the same object as the "" is. The "" will be referring to an object in the constant pool. Even if something is the empty string, it may not refer to the same entry in the constant pool.
    That is (something == "") could evaluate to false, but something.equals("") could return true.

  • Difference between rollback and issue_rollback(null)

    In key-Next-Item i kept issue_rollback(null) command,then application working fine.but when i keep just normal rollback command in the same place,it showing do u want to save changes message on my front_end application.
    So can any one tell that what is the diffrence between the normal ROLLBACK command and issue_rollback(null) procedure.

    Since the documentation warns against using Issue_Rollback outside of an On-Rollback trigger, it may not be doing anything.
    The plain rollback statement is interpreted by Forms as a Clear_Form with no parameters (which means using the default parameter values, which means it is doing a Clear_Form(Ask_commit,To_savepoint).
    If you really want to undo any changes already made by the user when he hits the Tab or Enter key, you should issue the Clear_Form with the proper parameters. And then you have the issue of re-querying the data.
    Maybe you should explain what you really want to happen in the trigger.

  • What is the difference between string != null and null !=string ?

    Hi,
    what is the difference between string != null and null != string ?
    which is the best option ?
    Thanks
    user8729783

    Like you've presented it, nothing.  There is no difference and neither is the "better option".

  • What is the difference between String Constant and Empty String Constant

    What is the difference between string constant which does not contain any value and the Empty string constant?
    While testing a VI which contain a normal string constant in VI analyzer, it gives error to change string constant with the empty string constant?
    Please Reply
    prabhakant
    Regards
    Prabhakant Patil

    Readability.
    Functionally, they are the same. From a coding standpoint, the Empty String Constant is unambiguous.
    It is empty and will always be; good for initialization. Also, because you can not type a value into and Empty String Constant, someone would need to conciously replace it to set a 'default' value that is something other than NULL.
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness

  • Difference between String and final String

    Hi friends,
    This is Ramana. Can u suggest me in this Question
    What is the difference between String and final String? Means
    String str="hai";
    final String str="hai";
    Regards,
    Ramana.

    *******REPEAT POST***********
    We already answered your question why post in a different section?
    http://forum.java.sun.com/thread.jspa?threadID=5201549

  • What Are the Differences Between String and StringBuffer?

    Both String and StringBuffer are final classes. StringBuffer grows in size dynamically. Could people help to detail other differences between String and StringBuffer?

    String is immutable. In other words, once it is made, the contents of that instance of String cannot be changed (easily). The size and characters cannot be changed, and code can take advantage of this fact to optimize space. Also, once you have a reference to a string, you do not need to worry about it changing at all, so you can eliminate constant tests to verify things such as size in another class that may want to hold onto the string.
    StringBuffer, as you noticed, can change dynamically. While this provides flexibility, it cannot be optimized and assumptions cannot be made as with strings. If a class holds a reference to a StringBuffer that it does not own, there is a possibility that it may change things such as size when the class tries to use it later. StringBuffer is also less efficient in handling space then an equivalent String. The character array to hold a String is exactly the length to hold all the characters. StringBuffer, on the other hand, adds a 16 character buffer of array space for possible expansions. Also, the size of the internal array doubles when its internal capacity is exceeded. So, unless you explicitly maintain the size, a StringBuffer will take up more memory then an equivalent String, and if your program needs thousands of instances of Strings (which is not uncommon), that space can add up.
    I hope this helps clarify some things for you.
    -JBoeing

  • What's the difference between String() - as String - toString();

    Hello,
    I'm wondering what's the difference between this 3 ways of getting around.

    There is a great difference between String(), as String and toString() methods.
    String(param) -- the more general and safe method.
    Returns "null" (string!) if param is null;
    Returns result of param.toString() method if it is defined in class;
    Returns string representation of the param (returns "[object Object]" if param is Object);
    param asString --
    Returns string if param is a type of String;
    Returns null otherway (if param is custom class, int or other type);
    param.toString() -- call of toString() member function
    throws an exception if param is null;
    compile-time error if toString() method is not defined in param (if your custom class do not have or inherits this function);
    returns result of toString() function
    Exists also ""+param and param+"" ways. They are similar to ""+String(param) and String(param)+""
    Pay attention:
    var ss:String = null;
    trace(String(ss)==(ss as String)); // Returns false as "null" not equal to null

  • Difference between -1 and null in the targettype filed

    Hi
    Could anyone advise what is the difference between -1 and null in the targettype filed in marketing documents?
    For example, in Sales Quotation - Rows (QUT1) table, I found for some open sales quotations, the field is null however the DB help file suggest the default value is -1 so I suppose it should be -1 for open documents.
    Many thanks for your advisory.

    Hi, Qian!
    I investigated that "-1" value appears in targettype field in Sales Quotation Rows (QUT1 table) when you Dublicate a Sales Quotation document (Ctrl + D). And NULL value appears when you create a new document without dublicating.
    In case you want to equal these 2 "empty"-values using T-SQL, you can use ISNULL(targettype, -1) function - this will give you -1 for both the "empty"-values
    HTH!
    Message was edited by: Aleksey Kuznetsov

  • Difference between void and null?

    wht is da difference between void and null w.r.t java?

    corlettk wrote:
    Why do you care, unless you're implementing a java compiler or JVM?Wow, you sure do suck at helping out in a forum. Why even make this post? You're not helping the OP any, and you made yourself look like a tool.
    To the op:
    Null is java's version of a null value. Java's version is more strict then many other languages, and will not work in a boolean expression or anywhere code expects a real and not null value. It's simply null.
    Void is java's way of declaring no return type on a method. Methods that are void take no 'return' statement and if one is provided will cause a fatal error. The exception to this is using 'return' without a value, which returns control to the caller of the method.
    Observe:
    //this method returns an int
    public int return_int(){
        int value = 5;
        return value;
    //this method does not return an int
    public void return_nothing(){
        int another_value = 123;
        System.out.println("Here's the value: " + return_int());
    //this method does not return anything
    public void nothing_returned(){
        return_nothing();
        return;
        System.out.println("This line never gets printed; the method returned control already!");
    }

  • Differences between awt and swing

    Ive written the following code, however Ive implemented the gui in awt rather then swing. Ive been told it hs too be in swing. What is the difference between swing and awt what will I need to change in my program so that its done using swing rather then the awt? Heres the code:
    // pp.java
    // Grant Brown
    // 22/02/02
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    // =========================================================
    // Class: pp
    // This class drives the customer manager. It contains the
    // main method which gets called as soon as this application
    // begins to run.
    // =========================================================
    class pp extends Frame implements ActionListener
    // Container of customer objects
    private Vector customers = new Vector (100);
    // List of names component. (Must specify java.awt in
    // front of List to distinguish the List class in the
    // java.awt package from the List class in the java.util
    // package.)
    private java.awt.List names = new java.awt.List ();
    // Delete and update button components.
    private Button delete;
    private Button update;
    // Default constructor.
    public pp ()
    // Assign "Paper Round Manager" to title bar of frame window.
    super ("Paper Round Manager");
    // Add a listener that responds to window closing
    // events. When this event occurs (by clicking on the
    // close box in the title bar), save customers and exit.
    addWindowListener (new WindowAdapter ()
    public void windowClosing
    (WindowEvent e)
    saveCustomers ();
    System.exit (0);
    // Place an empty label in the north part of the frame
    // window. This is done to correct an AWT positioning
    // problem.
    Label l = new Label ();
    add ("North", l);
    // Place the names component in the center part of the
    // frame window.
    add ("Center", names);
    // Create a panel object to hold four buttons.
    Panel p = new Panel ();
    Button b;
    // Add an Insert button to the Panel object and register
    // the current pp object as a listener for button events.
    p.add (b = new Button ("Insert"));
    b.addActionListener (this);
    // Add a Delete button to the Panel object and register
    // the current pp object as a listener for button events.
    p.add (delete = new Button ("Delete"));
    delete.addActionListener (this);
    // The Delete button should be disabled until there is at
    // least one customer to delete.
    delete.setEnabled (false);
    // Add an Update button to the Panel object and register
    // the current pp object as a listener for button events.
    p.add (update = new Button ("Update"));
    update.addActionListener (this);
    // The Update button should be disabled until there is at
    // least one customer to update.
    update.setEnabled (false);
    // Add a Finish button to the Panel object and register
    // the current customer object as a listener for button events.
    p.add (b = new Button ("Finish"));
    b.addActionListener (this);
    // Add the panel object to the frame window container.
    add ("South", p);
    // Set the background of the frame window container to
    // lightGray
    setBackground (Color.lightGray);
    // Set the size of the frame window container to 400
    // pixels horizontally by 200 pixels vertically.
    setSize (400, 200);
    // Allow the user to resize the frame window.
    setResizable (true);
    // Load all contacts.
    loadCustomers ();
    // Make sure that the frame window is visible.
    setVisible (true);
    public void actionPerformed (ActionEvent e)
    if (e.getActionCommand ().equals ("Delete"))
    delete ();
    else
    if (e.getActionCommand ().equals ("Finish"))
    saveCustomers ();
    System.exit (0);
    else
    if (e.getActionCommand ().equals ("Insert"))
    insert ();
    else
    update ();
    public Insets getInsets ()
    // Return an Insets object that describes the number of
    // pixels to reserve as a border around the edges of the
    // frame window.
    return new Insets (10, 10, 10, 10);
    public static void main (String [] args)
    // Create a new pp object and let it do its thing.
    new pp ();
    private void delete ()
    // Obtain index of selected contact item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot update
    // a contact if no contact item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Remove the contact item from the names component.
    names.remove (index);
    // Remove the Customer object from the contacts Vector
    // object.
    customers.remove (index);
    // If there are no more customers ...
    if (customers.size () == 0)
    delete.setEnabled (false);
    update.setEnabled (false);
    else
    // Make sure that the first contact item in the names
    // list is highlighted.
    names.select (0);
    private void insert ()
    // Create an Insert data entry form to enter information
    // for a new customer.
    DataEntryForm def = new DataEntryForm (this, "Insert");
    // If the bOk Boolean flag is set, this indicates the user
    // exited the form by pressing the Ok button.
    if (def.bOk)
    // Create a Contact object and assign information from
    // the form to its fields.
    Customer temp = new Customer ();
    temp.name = new String (def.name.getText ());
    temp.publication = new String (def.publication.getText ());
    temp.round = new String (def.round.getText ());
    temp.address = new String (def.address.getText ());
    temp.phone = new String (def.phone.getText ());
    // Add a new customer item to the names component.
    names.add (temp.name + ", " + temp.publication);
    // Add the Customer object to the contacts Vector
    // object.
    customers.add (temp);
    // Make sure that the Delete and Update buttons are
    // enabled.
    delete.setEnabled (true);
    update.setEnabled (true);
    // Destroy the dialouge box.
    def.dispose ();
    // Make sure that the first customer item in the names list
    // is highlighted.
    names.select (0);
    // ===========================================================
    // Load all contacts from contacts.dat into the contacts
    // Vector object. Also, make sure that the last name/first
    // name from each contact is combined into a String object and
    // inserted into the names component - as a contact item.
    // ===========================================================
    private void loadCustomers ()
    FileInputStream fis = null;
    try
    fis = new FileInputStream ("Customers.dat");
    DataInputStream dis = new DataInputStream (fis);
    int nCustomers = dis.readInt ();
    for (int i = 0; i < nCustomers; i++)
    Customer temp = new Customer ();
    temp.name = dis.readUTF ();
    temp.publication = dis.readUTF ();
    temp.round = dis.readUTF ();
    temp.address = dis.readUTF ();
    temp.phone = dis.readUTF ();
    names.add (temp.name + ", " + temp.publication);
    customers.add (temp);
    if (nCustomers > 0)
    delete.setEnabled (true);
    update.setEnabled (true);
    catch (IOException e)
    finally
    if (fis != null)
    try
    fis.close ();
    catch (IOException e) {}
    // Make sure that the first contact item in the names list
    // is highlighted.
    names.select (0);
    // ========================================================
    // Save all Customer objects from the customer Vector object
    // to customer.dat. The number of customerss are saved as an
    // int to make it easy for loadCustomers () to do its job.
    // ========================================================
    private void saveCustomers ()
    FileOutputStream fos = null;
    try
    fos = new FileOutputStream ("customers.dat");
    DataOutputStream dos = new DataOutputStream (fos);
    dos.writeInt (customers.size ());
    for (int i = 0; i < customers.size (); i++)
    Customer temp = (Customer) customers.elementAt (i);
    dos.writeUTF (temp.name);
    dos.writeUTF (temp.publication);
    dos.writeUTF (temp.round);
    dos.writeUTF (temp.address);
    dos.writeUTF (temp.phone);
    catch (IOException e)
    MsgBox mb = new MsgBox (this, "PP Error",
    e.toString ());
    mb.dispose ();
    finally
    if (fos != null)
    try
    fos.close ();
    catch (IOException e) {}
    private void update ()
    // Obtain index of selected customer item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot update
    // a customer if no customer item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Obtain a reference to the customer object (from the
    // customers Vector object) that is associated with the
    // index.
    Customer temp = (Customer) customers.elementAt (index);
    // Create and display an update entry form.
    DataEntryForm def = new DataEntryForm (this, "Update",
    temp.name,
    temp.publication,
    temp.round,
    temp.address,
    temp.phone);
    // If the user pressed Ok...
    if (def.bOk)
    // Update the customer information in the customers
    // Vector object.
    temp.name = new String (def.name.getText ());
    temp.publication = new String (def.publication.getText ());
    temp.round = new String (def.round.getText ());
    temp.address = new String (def.address.getText ());
    temp.phone = new String (def.phone.getText ());
    // Make sure the screen reflects the update.
    names.replaceItem (temp.name + ", " + temp.publication,
    index);
    // Destroy the dialouge box.
    def.dispose ();
    // Make sure that the first customer item in the names
    // list is highlighted.
    names.select (0);

    Ive doen pretty much what you said burt now my program isnt working at all. The window comes up but instead of doing something when you click the button it just throws shit loads of exceptions. Heres my abridged code
    // pp.java
    // Grant Brown
    // 22/02/02
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    // =========================================================
    // Class:PP
    // This class drives the Paper Round manager. It contains the
    // main method which gets called as soon as this application
    // begins to run.
    // =========================================================
    class pp extends JFrame implements ActionListener
    // Container of customer objects (one object per customer)
    private Vector customers = new Vector (100);
    // List of names component. (Must specify java.awt in
    // front of List to distinguish the List class in the
    // java.awt package from the List class in the java.util
    // package.)
    private java.awt.List names = new java.awt.List ();
    // Delete and update button components.
    private JButton delete;
    private JButton update;
    // Default constructor.
    public pp ()
    // Assign Contact Manager to title bar of frame window.
    super ("Paper Round Manager");
    // Add a listener that responds to window closing
    // events. When this event occurs (by clicking on the
    // close box in the title bar), save contacts and exit.
    addWindowListener (new WindowAdapter ()
    public void windowClosing
    (WindowEvent e)
    saveCustomers ();
    System.exit (0);
    // Place an empty label in the north part of the frame
    // window. This is done to correct an AWT positioning
    // problem. (One thing that you'll come to realize as
    // you work with the AWT is that there are lots of bugs.)
    JLabel l = new JLabel ();
    getContentPane().add ("North", l);
    // Place the names component in the center part of the
    // frame window.
    getContentPane().add ("Center", names);
    // Create a panel object to hold four buttons.
    JPanel p = new JPanel ();
    JButton b;
    // Add an Insert button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (b = new JButton ("Insert"));
    b.addActionListener (this);
    // Add a Delete button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (delete = new JButton ("Delete"));
    delete.addActionListener (this);
    // The Delete button should be disabled until there is at
    // least one contact to delete.
    delete.setEnabled (false);
    // Add an Update button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (update = new JButton ("Update"));
    update.addActionListener (this);
    // The Update button should be disabled until there is at
    // least one contact to update.
    update.setEnabled (false);
    // Add a Finish button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (b = new JButton ("Finish"));
    b.addActionListener (this);
    // Add the panel object to the frame window container.
    getContentPane().add ("South", p);
    // Set the background of the frame window container to
    // lightGray (to give a pleasing effect).
    setBackground (Color.lightGray);
    // Set the size of the frame window container to 400
    // pixels horizontally by 200 pixels vertically.
    setBounds (50, 100, 400, 200);
    // Do not allow the user to resize the frame window.
    loadCustomers ();
    // Load all contacts.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Make sure that the frame window is visible.
    setVisible (true);
    public void actionPerformed (ActionEvent e)
    if (e.getActionCommand ().equals ("Delete"))
    delete ();
    else
    if (e.getActionCommand ().equals ("Finish"))
    saveCustomers ();
    System.exit (0);
    else
    if (e.getActionCommand ().equals ("Insert"))
    insert ();
    else
    update ();
    public Insets getInsets ()
    // Return an Insets object that describes the number of
    // pixels to reserve as a border around the edges of the
    // frame window.
    return new Insets (10, 10, 10, 10);
    public static void main (String [] args)
    // Create a new cm object and let it do its thing.
    new pp ();
    private void delete ()
    // Obtain index of selected customer item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot update
    // a contact if no contact item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Remove the customer item from the names component.
    names.remove (index);
    // Remove the Customer object from the contacts Vector
    // object.
    customers.remove (index);
    // If there are no more customers ...
    if (customers.size () == 0)
    delete.setEnabled (false);
    update.setEnabled (false);
    else
    // Make sure that the first contact item in the names
    // list is highlighted.
    names.select (0);
    private void insert ()
    // Create an Insert data entry form to enter information
    // for a new contact.
    DataEntryForm def = new DataEntryForm (this, "Insert");
    // If the bOk Boolean flag is set, this indicates the user
    // exited the form by pressing the Ok button.
    if (def.bOk)
    // Create a Customer object and assign information from
    // the form to its fields.
    Customer temp = new Customer ();
    temp.name = new String (def.name.getText ());
    temp.publication = new String (def.publication.getText ());
    temp.address = new String (def.address.getText ());
    temp.round = new String (def.round.getText ());
    temp.phone = new String
              (def.phone.getText ());
    // Add a new customer item to the names component.
    names.add (temp.publication + ", " + temp.address);
    // Add the Customer object to the contacts Vector
    // object.
    customers.add (temp);
    // Make sure that the Delete and Update buttons are
    // enabled.
    delete.setEnabled (true);
    update.setEnabled (true);
    // Destroy the dialog box.
    def.dispose ();
    // Make sure that the first customer item in the names list
    // is highlighted.
    names.select (0);
    // ===========================================================
    // Load all customers from customers.dat into the customers
    // Vector object. Also, make sure that the last name/first
    // name from each contact is combined into a String object and
    // inserted into the names component - as a contact item.
    // ===========================================================
    private void loadCustomers ()
    FileInputStream fis = null;
    try
    fis = new FileInputStream ("customers.dat");
    DataInputStream dis = new DataInputStream (fis);
    int nCustomers = dis.readInt ();
    for (int i = 0; i < nCustomers; i++)
    Customer temp = new Customer ();
    temp.name = dis.readUTF ();
    temp.publication = dis.readUTF ();
    temp.address = dis.readUTF ();
    temp.round = dis.readUTF ();
    temp.phone = dis.readUTF ();
    names.add (temp.publication + ", " + temp.address);
    customers.add (temp);
    if (nCustomers > 0)
    delete.setEnabled (true);
    update.setEnabled (true);
    catch (IOException e)
    finally
    if (fis != null)
    try
    fis.close ();
    catch (IOException e) {}
    // Make sure that the first customer item in the names list
    // is highlighted.
    names.select (0);
    // ========================================================
    // Save all customer objects from the contacts Vector object
    // to customers.dat. The number of contacts are saved as an
    // int to make it easy for loadCustomers () to do its job.
    // ========================================================
    private void saveCustomers ()
    FileOutputStream fos = null;
    try
    fos = new FileOutputStream ("customers.dat");
    DataOutputStream dos = new DataOutputStream (fos);
    dos.writeInt (customers.size ());
    for (int i = 0; i < customers.size (); i++)
    Customer temp = (Customer) customers.elementAt (i);
    dos.writeUTF (temp.name);
    dos.writeUTF (temp.publication);
    dos.writeUTF (temp.address);
    dos.writeUTF (temp.round);
    dos.writeUTF (temp.phone);
    catch (IOException e)
    MsgBox mb = new MsgBox (this, "PP Error",
    e.toString ());
    mb.dispose ();
    finally
    if (fos != null)
    try
    fos.close ();
    catch (IOException e) {}
    private void update ()
    // Obtain index of selected customer item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot update
    // a customer if no customer item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Obtain a reference to the Customer object (from the
    // customer Vector object) that is associated with the
    // index.
    Customer temp = (Customer) customers.elementAt (index);
    // Create and display an update entry form.
    DataEntryForm def = new DataEntryForm (this, "Update",
    temp.name,
    temp.publication,
    temp.address,
    temp.round,
    temp.phone);
    // If the user pressed Ok...
    if (def.bOk)
    // Update the customer information in the customers
    // Vector object.
    temp.name = new String (def.name.getText ());
    temp.publication = new String (def.publication.getText ());
    temp.address = new String (def.address.getText ());
    temp.round = new String (def.round.getText ());
    temp.phone = new String (def.phone.getText ());
    // Make sure the screen reflects the update.
    names.replaceItem (temp.publication + ", " + temp.address,
    index);
    // Destroy the dialog box.
    def.dispose ();
    // Make sure that the first customer item in the names
    // list is highlighted.
    names.select (0);
    // ========================================================
    // Class: Customer
    // This class describes the contents of a business customer.
    // ========================================================
    class Customer
    public String name;
    public String publication;
    public String address;
    public String round;
    public String phone;
    // ==========================================================
    // Class: DataEntryForm
    // This class provides a data entry form for entering customer
    // information.
    // ==========================================================
    class DataEntryForm extends JDialog implements ActionListener
    // bOk is a boolean flag. When true, it indicates that
    // the Ok button was pressed to terminate the dialog box
    // (as opposed to the Cancel button).
    public boolean bOk;
    // The following components hold the text that the user
    // entered into the visible text fields.
    public JTextField name;
    public JTextField publication;
    public JTextField address;
    public JTextField round;
    public JTextField phone;
    public void actionPerformed (ActionEvent e)
    // If the user pressed the Ok button, indicate this
    // by assigning true to bOk.
    if (e.getActionCommand ().equals ("Ok"))
    bOk = true;
    // Destroy the dialog box and return to the point
    // just after the creation of the DataEntryForm object.
    dispose ();
    public DataEntryForm (JFrame parent, String title)
    // Call the other constructor. The current constructor
    // is used for insert operations. The other constructor
    // is used for update operations.
    this (parent, title, "", "", "", "", "");
    public DataEntryForm (JFrame parent, String title,
    String name, String publication,
    String address, String round,
    String phone)
    // Initialize the superclass layer.
    super (parent, title, true);
    // Choose a grid bag layout so that components can be more
    // accurately positioned. (It looks nicer.)
    setLayout (new GridBagLayout ());
    // Add appropriate first name, last name, phone, fax, and
    // email components to the current DataEntryForm container.
    // (Remember, DataEntryForm is a subclass of Dialog.
    // Dialog is a container. Therefore, DataEntryForm
    // inherits the ability to be a container.)
    addComponent (this, new JLabel ("Name: "), 0, 0, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.name = new JTextField (15);
    addComponent (this, this.name, 1, 0, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("Update"))
    this.name.setText (name);
    addComponent (this, new JLabel ("Publications: "), 0, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.publication = new JTextField (15);
    addComponent (this, this.publication, 1, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("Update"))
    this.publication.setText (publication);
    addComponent (this, new JLabel ("Address "), 0, 2, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.address = new JTextField (15);
    addComponent (this, this.address, 1, 2, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("Update"))
    this.address.setText (address);
    addComponent (this, new JLabel ("Round No "), 0, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.round = new JTextField (15);
    addComponent (this, this.round, 1, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("Update"))
    this.round.setText (round);
    addComponent (this, new JLabel ("Phone Number "), 0, 4, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.phone = new JTextField (15);
    addComponent (this, this.phone, 1, 4, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("Update"))
    this.phone.setText (phone);
    addComponent (this, new JLabel (""), 0, 5, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    addComponent (this, new JLabel (""), 1, 5, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    JButton b;
    // Add an Ok button to this container.
    addComponent (this, b = new JButton ("Ok"), 0, 6, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    b.addActionListener (this);
    // Add a Cancel button to this container.
    addComponent (this, b = new JButton ("Cancel"), 1, 6, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    b.addActionListener (this);
    // Set the size of the dialog window to 250 pixels
    // horizontally by 200 pixels vertically.
    setSize (250, 200);
    // Do not allow users to resize the dialog window.
    setResizable (false);
    // Make sure that the dialog window is visible.
    setVisible (true);
    private void addComponent (Container con, Component com,
    int gridx, int gridy,
    int gridw, int gridh, int fill,
    int anchor)
    // Get the current layout manager. It is assumed to
    // be a GridBagLayout object.
    LayoutManager lm = con.getLayout ();
    // Create a GridBagConstraints object to make it
    // possible to customize component positioning.
    GridBagConstraints gbc = new GridBagConstraints ();
    // Assign the x and y grid positions.
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    // Assign the number of grid blocks horizontally and
    // vertically that are occupied by the component.
    gbc.gridwidth = gridw;
    gbc.gridheight = gridh;
    // Specify the component's resize policy (fill) and
    // the direction in which the component is positioned
    // when its size is smaller than available space (anchor).
    gbc.fill = fill;
    gbc.anchor = anchor;
    // Set the new constraints that the grid bag layout
    // manager will use.
    ((GridBagLayout) lm).setConstraints (com, gbc);
    // Add the component to the container.
    con.add (com);
    // ===========================================================
    // Class: MsgBox
    // This class displays a message box to the user. The message
    // is usually an error message. The user must press the Ok
    // button to terminate the message box.
    // ===========================================================
    class MsgBox extends JDialog implements ActionListener
    public void actionPerformed (ActionEvent e)
    // Terminate the dialog box in response to the user
    // pressing the Ok button.
    dispose ();
    public MsgBox (JFrame parent, String title, String msg)
    // Initialize the superclass layer.
    super (parent, title, true);
    // Store the msg argument in a Label object and add
    // this object to the center part of the dialog window.
    JLabel l = new JLabel (msg);
    add ("Center", l);
    // Create a Button object and add it to the south part
    // of the dialog window.
    JButton b = new JButton ("Ok");
    add ("South", b);
    // Make the current object a listener to events that
    // occur as a result of the user pressing the Ok
    // button.
    b.addActionListener (this);
    // Make sure that the Ok button has the focus.
    b.requestFocus ();
    // Do not allow users to resize the dialog window.
    setResizable (false);
    // Allow the layout manager to choose an appropriate
    // size for the dialog window.
    pack ();
    // Make sure that the dialog window is visible.
    setVisible (true);
    }

  • Re: The difference between update() and doFinal() in ClassCipher

    Hi Guys,
    I found this topic,
    The difference between update() and doFinal() in ClassCipher
    during a Google Search. I need your help, because I'm working on a project for an university exam.
    It's about a Client FTP that sends and received files with the content crypted.
    I'm using DES Encryption with "DES/CBC/PKCS5Padding".
    I have this doubt: when it's correct to uso the doFinal method? At the last block of the file, or after it? (for encryption)
    And during the decryption, how can I use the result of the doFinal operation?

    Reposting with &#123;code} tags
    capagira87 wrote:
    Ok these are the functions:
    public void CodificaInvia (String cd, String filename, SecretKey Key) // it opens, codes and sends to the server the content of the file 512 bytes per time
    int returncode=0;
    Socket DataSock = null;
    FileInputStream fis = null;
    byte [] text = null;
    try {
    DataSock = new Socket (servAddress,22);
    ObjectInputStream cin = new ObjectInputStream (DataSock.getInputStream());
    ObjectOutputStream out = new ObjectOutputStream (DataSock.getOutputStream());
    byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, Key, paramSpec);
    fis = new FileInputStream (cd+File.separator+filename);
    int size = fis.available();
    if ((size%8)==0)
    text = new byte [size];
    } else {
    do {
    size++;
    } while ((size%8)!=0);
    text = new byte [size];
    fis.read(text,0,fis.available());
    byte [] ciphertext = cipher.doFinal(text);
    out.writeInt(ciphertext.length);
    int numblock = ciphertext.length/512;
    int resto = ciphertext.length%512;
    out.writeInt(numblock);
    out.writeInt(resto);
    byte [] tempblock = new byte [512];
    for (int conta=0; conta<numblock; conta++)
    for (int i=0; i<512; i++)
    tempblock=ciphertext[(conta*512)+i];
    out.write(tempblock);
    out.flush();
    if (resto > 0) // this block works if the file size is not multiple of 512
    java.util.Arrays.fill (tempblock, (byte) 0);
    int h= numblock*512;
    for (int i=0; i<resto; i++)
    tempblock[i]=ciphertext[h+i];
    out.write (tempblock);
    out.flush();
    System.out.println ("OK!");
    } catch (Exception e)
    e.printStackTrace();
    public void RicezioneDecodifica (String filename, SecretKey Key) // in this function I receive the file blocks, I decrpyt them and I save the decrypted text into a file
    Socket DataSock = null;
    byte [] buffer=null; // it saves the received data
    String Plaintext = new String (); // it saves the deciphered text
    try {
    ServerSocket ss2 = new ServerSocket (TRANSMISSION_PORT);
    DataSock = ss2.accept();
    ObjectOutputStream out = new ObjectOutputStream (DataSock.getOutputStream());
    ObjectInputStream cin = new ObjectInputStream (DataSock.getInputStream());
    byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    Cipher dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    dcipher.init(Cipher.DECRYPT_MODE, Key, paramSpec);
    int size = cin.readInt();
    int numblock = cin.readInt();
    int resto = cin.readInt();
    if ((size%8)!=0)
    do{
    size++;
    } while ((size%8)!=0);
    int ByteLetti=0; // counter of read bytes
    buffer = new byte [size];
    for (int conta=0; conta<numblock; conta++)
    int a=cin.available();
    byte [] tempblock = new byte [a];
    cin.read(tempblock, 0, a);
    for (int i=0; i<a; i++)
    buffer[ByteLetti+i]=tempblock[i];
    ByteLetti=ByteLetti+a;
    byte[] texttmp=dcipher.update(tempblock);
    String tmp = new String (texttmp, "UTF8");
    Plaintext=Plaintext+tmp;
    if (resto>0)
    int a = cin.available();
    byte [] tempblock;
    if ((a%8)>0)
    do {
    a++;
    } while ((a%8)!=0);
    tempblock = new byte [a];
    cin.read(tempblock, 0, cin.available());
    byte[] texttmp = dcipher.doFinal(tempblock);
    String tmp = new String (texttmp, "UTF8");
    Plaintext = Plaintext+tmp;
    ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (filename));
    oos.writeChars(Plaintext);
    oos.flush();
    oos.close();
    } catch (Exception e)
    e.printStackTrace();

  • What's the difference between equals() and compareTo() method

    I'm confused between the two method from String class ,,, what's the main difference between equals() and compareTo() method?

    API docs give quite clear information IMHO
    public boolean equals(Object anObject)
    Compares this string to the specified object. The result is true if and only if the argument is not null and is
    a String object that represents the same sequence of characters as this object.
    public int compareTo(String anotherString)
    Compares two strings lexicographically. ....
    The result is a negative integer if this String object lexicographically precedes the argument string. The
    result is a positive integer if this String object lexicographically follows the argument string. The result is
    zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return
    true. Mike

  • Difference between GUI_UPLOAD and WS_UPLOAD

    Hi,
    Please make me clear about the difference between GUI_UPLOAD and WS_UPLOAD. In which cases we need to use these modules...??
    Thanks,
    Satish

    I would suggest to always use the GUI_UPLOAD.  I say this because this is the function module which is used in the GUI_UPLOAD method of the class CL_GUI_FRONTEND_SERVICES.   Really, you should probably use the class/method instead of the function module.
      data: filename type string.
      filename = p_file.
      call method cl_gui_frontend_services=>gui_upload
             exporting
                  filename                = filename
                  filetype                = 'ASC'
             changing
                  data_tab                = iflatf
             exceptions
                  file_open_error         = 1
                  file_read_error         = 2
                  no_batch                = 3
                  gui_refuse_filetransfer = 4
                  no_authority            = 6
                  unknown_error           = 7
                  bad_data_format         = 8
                  unknown_dp_error        = 12
                  access_denied           = 13
                  others                  = 17.
    Regards,
    Rich Heilman

  • Difference between schema and DTD

    Difference between schema and DTD
    <author>
    <firstname></firstname>
    <lastname></lastname>
    </author>
    How will u write dtd and schema for above XML ?

    DTD:
    <!ELEMENT author(firstname, lastname)>
    <!ELEMENT firstname(#PCDATA)>
    <!ELEMENT lastname(#PCDATA)>
    Schema:
    <xs:element name="author">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

  • Difference between CHAR and VARCHAR2 datatype

    Difference between CHAR and VARCHAR2 datatype
    CHAR datatype
    If you have an employee name column with size 10; ename CHAR(10) and If a column value 'JOHN' is inserted, 6 empty spaces will be inserted to the right of the value. If this was a VARCHAR column; ename VARCHAR2(10). How would it handle the column value 'JOHN' ?

    The CHAR datatype stores fixed-length character strings, and Oracle compares CHAR values using blank-padded comparison semantics.
    Where as the VARCHAR2 datatype stores variable-length character strings, and Oracle compares VARCHAR2 values using nonpadded comparison semantics.
    This is important when comparing or joining on the columns having these datatypes;
    SQL*Plus: Release 10.2.0.1.0 - Production on Pzt Au 6 09:16:45 2007
    Copyright (c) 1982, 2005, Oracle. All rights reserved.
    SQL> conn hr/hr
    Connected.
    SQL> set serveroutput on
    SQL> DECLARE
    2 last_name1 VARCHAR2(10) := 'TONGUC';
    3 last_name2 CHAR(10) := 'TONGUC';
    4 BEGIN
    5 IF last_name1 = last_name2 THEN
    6 DBMS_OUTPUT.PUT_LINE ( '-' || last_name1 || '- is equal to -' || last_name2
    || '-');
    7 ELSE
    8 DBMS_OUTPUT.PUT_LINE ( '-' || last_name1 || '- is NOT equal to -' || last_n
    ame2 || '-');
    9 END IF;
    10 END;
    11 /
    -TONGUC- is NOT equal to -TONGUC -
    PL/SQL procedure successfully completed.
    SQL> DECLARE
    2 last_name1 CHAR(6) := 'TONGUC';
    3 last_name2 CHAR(10) := 'TONGUC';
    4 BEGIN
    5 IF last_name1 = last_name2 THEN
    6 DBMS_OUTPUT.PUT_LINE ( '-' || last_name1 || '- is equal to -' || last_name2
    || '-');
    7 ELSE
    8 DBMS_OUTPUT.PUT_LINE ( '-' || last_name1 || '- is NOT equal to -' || last_n
    ame2 || '-');
    9 END IF;
    10 END;
    11 /
    -TONGUC- is equal to -TONGUC -
    PL/SQL procedure successfully completed.
    Also you may want to read related asktom thread - "Char Vs Varchar" http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1542606219593
    and http://tahitiviews.blogspot.com/2007/05/less-is-more-more-or-less.html
    Best regards.

Maybe you are looking for