Structs / Records in Java

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    Structs / Records in Java

    Hi,

    We all know that Java has classes but how about basic storage objects like
    structs? C and C++ have Structs, Pascal has Records, Visual Basic has Types
    etc. How about Java?

    Greetings,
    Rick



  • Jonas Kongslund

    #2
    Re: Structs / Records in Java

    Rick wrote:
    [color=blue]
    > Hi,
    >
    > We all know that Java has classes but how about basic storage objects like
    > structs? C and C++ have Structs, Pascal has Records, Visual Basic has
    > Types etc. How about Java?[/color]

    Java does not support structs and records. Use classes instead.

    --
    Jonas Kongslund

    Comment

    • Rick

      #3
      Re: Structs / Records in Java

      Ok, thats clear. Thanks!

      Rick


      Comment

      • Stewart Gordon

        #4
        Re: Structs / Records in Java



        While it was 3/11/03 10:10 am throughout the UK, Jonas Kongslund
        sprinkled little black dots on a white screen, and they fell thus:

        <snip>[color=blue]
        > Java does not support structs and records.[/color]
        <snip>

        Yes it does. It just calls them classes, and greatly expands upon their
        functionality.

        Even in C++, a struct _is_ a class. There, the struct keyword just
        serves for backward compatibility and/or syntactic sugar depending on
        your point of view.

        Stewart.

        --
        My e-mail is valid but not my primary mailbox. Please keep replies on
        on the 'group where everyone may benefit.

        Comment

        • Amey Samant

          #5
          Re: Structs / Records in Java

          "Rick" <aso3rick@hotma il.com> wrote in message news:<3fa629f6$ 0$58706$e4fe514 c@news.xs4all.n l>...[color=blue]
          > Ok, thats clear. Thanks!
          >
          > Rick[/color]

          hey
          structs in C++ are just classes with default access specifier as
          public (otherwise there is no differnec in class in C++ & a struct in
          C++)
          thus if you need to store only data, java does not force you to have
          methods in class
          for eg-: while creating data structures you can create class for Node
          class Node
          {
          int data;
          Node next;
          }
          this is equivalent to structs .... isnt it ?

          regards
          amey

          Comment

          • Kenneth Rose

            #6
            Re: Structs / Records in Java

            Amey Samant wrote:[color=blue]
            > class Node
            > {
            > int data;
            > Node next;
            > }
            > this is equivalent to structs .... isnt it ?[/color]

            Nope... default access for instance variables is protected, not public.
            Should be:

            class Node
            {
            public int data;
            public Node next;
            }

            /<en

            Comment

            • Rick

              #7
              Re: Structs / Records in Java

              I'm not 100 % sure but I don't think you may say a struct is the same as a
              class. Of course, they both can be used for storing data. Classes are some
              sort of advanced struct types, based on structs (since OOP languages like
              Java and C++ languages came after non-object-orientated languages like C
              which already had structs that time). So, they won't differ that much but
              it's the same as :
              "A lion is a beast but a beast is not a lion." (what a stupid example :| ).

              It doesn't matter really much, I can live with the Java classes, but I was
              just curious. Or maybe I was lazy because using structs if a little bit
              simpler because you don't have to create them like classes.

              Greetings,
              Rick


              Comment

              • Amey Samant

                #8
                Re: Structs / Records in Java

                Stewart Gordon <smjg_1998@yaho o.com> wrote in message news:<bo5v2v$1c i$1@sun-cc204.lut.ac.uk >...

                hi stewart
                [color=blue]
                > Even in C++, a struct _is_ a class.
                > There, the struct keyword just
                > serves for backward compatibility and/or syntactic sugar depending on
                > your point of view.[/color]

                no structs in C++ are different than class (the only difference is in
                class by default access specifier is private whereas in structs it is
                public)
                otherwise they are same
                regards
                amey

                Comment

                • Stewart Gordon

                  #9
                  OT: Structs / classes in C++ (was: Structs / Records in Java)



                  While it was 4/11/03 10:01 am throughout the UK, Amey Samant sprinkled
                  little black dots on a white screen, and they fell thus:

                  <snip>[color=blue]
                  > no structs in C++ are different than class (the only difference is in
                  > class by default access specifier is private whereas in structs it is
                  > public)[/color]
                  <snip>

                  I didn't say that the C++ keywords 'struct' and 'class' were synonyms.
                  I merely meant that by defining a struct, you are defining a class, and
                  it just saves having to put public: at the top.

                  Although it isn't enforced by the language, it remains common practice
                  (IINM) to use 'struct' for C-style structs, and 'class' when you want to
                  use the functionality of a class.

                  Basically,

                  struct Qwert {

                  means exactly the same as

                  class Qwert {
                  public:

                  and would compile to precisely the same.

                  Stewart.

                  --
                  My e-mail is valid but not my primary mailbox. Please keep replies on
                  on the 'group where everyone may benefit.

                  Comment

                  Working...