Thread: [Java basics #1] Conversion.

Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1 [Java basics #1] Conversion. 
    Registered Member
    blackaddiction's Avatar
    Join Date
    Feb 2008
    Age
    33
    Posts
    539
    Thanks given
    10
    Thanks received
    14
    Rep Power
    170
    What i'm teaching:

    The basics on how to convert Strings into other things & convert classes or Objects into other classes.


    1:

    Strings to others:

    We call three Strings:
    Code:
    private String i = "3";
    private String d = "0.3";
    private String b = "true";
    We want the Strings to behave as other types.
    So this is what we are going to do:
    Code:
    private int I = Integer.parseInt(i);
    Thats calling a new integer named I and parsing a String (converting) into a integer.
    The same style goes throughout:
    Code:
    private double D = Double.parseDouble(d);
    private boolean B = Boolean.parseBool(b);
    2:

    Classes / Objects to other Classes:

    Okay this is very simple. To convert classes into other classes simply do this
    Code:
    Class1 = (Class1)Class2;
    Please note the classes must have SOME relation for this to work.

    You can also call Objects to store them as classes, this is also simple.
    Simpley call an Object like so:
    Code:
    private Object obj;
    Then inside a method or while calling the Object set its value.
    Personally I like getters and setters but this way works just the same.
    Code:
    public void ChangeObj() {
    obj = Class1;
    }
    //Or
    private Object obj = Class1;
    Thank you for reading, I will be posting more of these basic java tutorials and a few intermediate and advanced ones.
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    You haven't said WHY they'd do it.
    Reply With Quote  
     

  3. #3  
    Registered Member
    blackaddiction's Avatar
    Join Date
    Feb 2008
    Age
    33
    Posts
    539
    Thanks given
    10
    Thanks received
    14
    Rep Power
    170
    Could be used in a range of ways, loading configuration files, etc.

    EDIT:

    And for Objects storing classes could be used for things such as, differencing between NPCs and humans but still using the same classes
    Reply With Quote  
     

  4. #4  
    Registered Member

    Join Date
    Oct 2009
    Age
    28
    Posts
    391
    Thanks given
    19
    Thanks received
    4
    Rep Power
    159
    Class1 = (Class1)Class2;
    This is called casting..
    In the world of computer science, the term user, means idiot.
    -Benjamin.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Registered Member Paketa's Avatar
    Join Date
    Oct 2007
    Posts
    2,681
    Thanks given
    17
    Thanks received
    82
    Rep Power
    680
    Good job
    Reply With Quote  
     

  7. #6  
    Registered Member
    blackaddiction's Avatar
    Join Date
    Feb 2008
    Age
    33
    Posts
    539
    Thanks given
    10
    Thanks received
    14
    Rep Power
    170
    Quote Originally Posted by Benjamin View Post
    This is called casting..
    But it is still in essence converting bud'.
    Reply With Quote  
     

  8. #7  
    Registered Member

    Join Date
    Oct 2009
    Age
    28
    Posts
    391
    Thanks given
    19
    Thanks received
    4
    Rep Power
    159
    Quote Originally Posted by blackaddiction View Post
    But it is still in essence converting bud'.
    I know, I just said it to let people know the technical term, so they know what it means, if they read it on the internet.
    In the world of computer science, the term user, means idiot.
    -Benjamin.
    Reply With Quote  
     

  9. #8  
    Registered Member

    Join Date
    Sep 2009
    Posts
    106
    Thanks given
    0
    Thanks received
    2
    Rep Power
    77
    Casting, bro.
    Reply With Quote  
     

  10. #9  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Put Float in there and indent.
    gj.
    Reply With Quote  
     

  11. #10  
    Registered Member
    Join Date
    Nov 2009
    Posts
    13
    Thanks given
    0
    Thanks received
    0
    Rep Power
    23
    Good job.
    [C]hargerz

    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •