wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Is an int array (int[]) apart of the Java Collections or Java Library? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is an int array (int[]) apart of the Java Collections or Java Library?" Watch "Is an int array (int[]) apart of the Java Collections or Java Library?" New topic
Author

Is an int array (int[]) apart of the Java Collections or Java Library?

Luke Winter
Greenhorn

Joined: Aug 18, 2012
Posts: 5
Hello,

I have an assignment for class and the teacher has explicitly said No use of Java libraries, or any other libraries. (That precludes, for instance, the use of any Java Collections.) This means that NO IMPORT CLAUSES ARE ALLOWED.

I have used:



I didn't use any imports and from what I can see, is not apart of the Java Collections or Libraries. I just want to make sure otherwise I get a 0. Is it?

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Arrays are a built-in Java language feature. They're not part of the Java collection classes or standard Java library.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Make sure you also don't use anything from java.lang. These don't need to be imported but are part of the core library. That means you can't use String, Object, Integer etc.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Luke Winter
Greenhorn

Joined: Aug 18, 2012
Posts: 5
Rob Spoor wrote:Make sure you also don't use anything from java.lang. These don't need to be imported but are part of the core library. That means you can't use String, Object, Integer etc.


Thank-you Rob!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Luke Winter wrote:
Rob Spoor wrote:Make sure you also don't use anything from java.lang. These don't need to be imported but are part of the core library. That means you can't use String, Object, Integer etc.
Thank-you Rob!

Hmmm. It depends what your tutor's 'rule' actually means. Is it "no import statements" or "no use of any libraries"?
Rob's quite right that java.lang is a library; but not allowing it means that you can't use any objects at all except arrays (because Object itself is also part of java.lang), which seems an odd way to learn an Object-Oriented language.

You might want to check with him/her to make sure exactly what the rule means.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

The teaching goal of this exercise completely eludes me. "Here's a bicycle, learn how to ride it but don't use the pedals." WTH?


Junilu - [How to Ask Questions] [How to Answer Questions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I also wonder how strict the rules are - because really, it's impossible to write a Java program without using anything from the java.lang package at all.

Look at this super simple program.

What could be "wrong" with this?

  • Class Example implicitly extends java.lang.Object. So, we're already (implictly) using a class from java.lang there.
  • The main method takes a String[] as its argument. That's just how you have to write it. No escaping from using class java.lang.String here.
  • Class System is also a class from the java.lang package. If you were not allowed to use this, your program couldn't print any output at all.

  • Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19654
        
      18

    Don't forget the PrintStream (from java.io!) you're using there.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4343
        
        8

    Of course, if the rule was just "no imports", you can fully qualify all your class names and use whatever you want!
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11161
        
      16

    This assignment seems about as useful as

    Can you write a program that doesn't use the letter 'e' in it anywhere. Even if you can, what would be the point?


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4343
        
        8

    More seriously, I suspect it's a clumsy way of phrasing an exercise in using arrays and loops to stop people using collection classes that would make life too easy and side-step the things that are actually being tested.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7549
        
      18

    Jesper de Jong wrote:Class Example implicitly extends java.lang.Object...

    Which raises an interesting point. Does xxx[] actually extend java.lang.Object or not? I suspect so since, as we all know, arrays are objects; but since it's part of the language it could simply be a structure dressed up to look like an Object.

    I suspect it's stated in the JLS somewhere; I've just never had reason to find out.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38020
        
      22
    Winston Gutkowski wrote: . . . Does xxx[] actually extend java.lang.Object or not? . . . I suspect it's stated in the JLS somewhere; . . . Winston
    Of course it is in the JLS.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14074
        
      16

    Winston Gutkowski wrote:Does xxx[] actually extend java.lang.Object or not? I suspect so since, as we all know, arrays are objects; but since it's part of the language it could simply be a structure dressed up to look like an Object.

    That's really a theoretical discussion for which I'm not sure if there's an answer, but it doesn't matter. If an int[] looks like an Object, behaves like an Object, has all the methods of class Object, then does it matter if it really extends Object or if the compiler just makes it look like an Object? What does the difference even mean if there's no practical difference when using arrays?

    The JLS paragraph that Campbell linked to says that arrays inherit the members from Object, which seems to imply that arrays indeed extend Object.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38020
        
      22
    …And if you scroll up, it says you can assign any array to an Object reference. If you scroll down there is an example where it prints the name of the superclass.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7549
        
      18

    Jesper de Jong wrote:If an int[] looks like an Object, behaves like an Object, has all the methods of class Object, then does it matter if it really extends Object or if the compiler just makes it look like an Object?

    Because of the rather ambiguous "rules" that started off this whole thread. It may be splitting hairs to you, but it might mean a 0 for poor Luke.

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Is an int array (int[]) apart of the Java Collections or Java Library?
     
    Similar Threads
    Main advantage of Grails over RoR apart from 'Java like' syntax?
    Java XMLRPC
    Beginning Android Games. How proficient a programmer one needs to be to start?
    Parse string and make every URL into hyperlink
    collections and generics