Results 1 to 7 of 7
  1. #1
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Smile Real nooby question but oh well =P

    How does the declaration for vars go again...

    width = new double;
    double width = new double;

    :confused:

  2. #2
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    double width; //assigns default value
    or
    double width = 13;

  3. #3
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Wait, then what's with the someclass name = new someclass thing? What's with the 'new'?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not too experienced with java, but I know the answer to this question.

    int n = 10; sets up an integer value to be added to memory.

    classname x = new clasname;
    creates a new class reference in memory. Its basically like creating a new version of some class to manipulate.

  5. #5
    Arthur123 is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Hmm... I sort of get it, thanks. :)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is, what I hope, is a better explanation. Say you have a class that handles Fractions. It allows you to add fractions, print fractions, mult, div, sub, and anything else you could think of.

    Now say you want to be able to add two fraction objects together.

    What you would do is first set up 3 fractions, 2 for the fractions you are adding and one for the result of the calculation.

    how would you create these fractions?

    you want to call

    Java Code:
    Fraction fract1 = new Fraction();
    Fraction fract2 = new Fraction();
    Fraction fract3 = new Fraction();
    Applying that in your main function gives you three objects to work with.
    It puts them into memory, so you can manipulate them.

    From there you can use a fraction method to set values
    Java Code:
    fract1.set(10, 20);
    fract2.set(20, 30);
    fract3 = fract1 + fract2;
    set function sets the numerator, denominator and reduces if possible.
    10/20 = 1/2

    This functionality may work a little differently in Java, I got this explanation from a c++ book. The idea is similar still.
    Last edited by sunde887; 01-05-2011 at 12:44 AM.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    sunde is right.

    Basically, when you use "int x = 5;" or "double x = 5.0;" or similar, you're creating a variable of primitive type. This can later be referenced and reassigned, using "x = 6;" (for the int version) or "x = 6.0;" (for the double version). More on primitives here: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    When you use the "new" syntax, i.e. "String x = new String("abc");", you're legitimately creating a new object in the memory. It has several methods and values attached to it, as defined in its class. When you create a class, most of the time you use it to create an object of that type that can later be used. If you created a class called "abc", and wanted a new object of that type, it would be "abc x = new abc();". Reassigning it later, again, would drop the type from the beginning, and just be "x = new abc();". More on objects here: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language) and What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    I would stray away from the part of sunde's example that says "fract3 = fract1 + fract2". This is a behavior in C++ (and a few other languages) called Operator Overloading, but it is not available in Java. Everything is done through calling methods on objects.

Similar Threads

  1. Real Time?
    By RaustBD in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 03:14 AM
  2. How to update in real time
    By darius_dada in forum AWT / Swing
    Replies: 1
    Last Post: 12-14-2010, 11:05 PM
  3. Real Newbie Question - I'm sure its dumb
    By rulian in forum New To Java
    Replies: 4
    Last Post: 12-11-2008, 05:07 PM
  4. Need help real bad on 2D array please !
    By TrungTran in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 05:04 AM
  5. Extreme nooby having hard time with this app
    By asterix350z in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 08:24 PM

Posting Permissions

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