Import of Arrays util not working - java

I need to import the Arrays utilities for use in some code I am writing, however, when I type in "import java.util.Arrays" , the word "Arrays" does not turn blue. I'm guessing this means I can not import it but why is this? Since the word does not turn blue does that indicate that I do not have the package or whatever? Should it not be included in the java files I downloaded when installing Jcreator?

the package is util not utils so import
import java.util.Arrays
Docs

You have to use import java.util.Arrays; or import java.util.*. The latter imports all the classes within the util package.

Related

java.util.logging does not exist bug?

I am trying to import java.util.logging into my class but it doesn't exist.
I am using Netbeans 8.2 installed just a few weeks ago, when I started learning Java and Netbeans.
I found this old bug report Bugzilla report but this is so old, I would expect it to be fixed, especially for a logging function.
The following is the only code I have in the class file. The 2 java.awt classes import OK. I added those just to prove that I had access to the standard Java library. The auto suggest does not suggest anything starting with log...
package myflag.flag.logging;
import java.util.logging;
import java.awt.Color;
import java.awt.Font;
Any ideas please???
Only a type can be imported. java.util.logging is a package. Please specify directly what class you need to import or you can import like this for all class inside this package: import java.util.logging.*;

What about using import java.* for using all sub-packages under the package 'java'?

import java.*;
Why cannot I do this import? Instead of importing all classes in a particular sub-package of the package 'java', I tried to import all the sub-packages under the the 'java' package.
How can I import all the built-in classes?
There is no such thing as sub-package in java.
java.util.stream is not a sub-pacakge of java.util.
Therefore import java.util.* doesn't import the classes of java.util.stream.
To import all the built in classes, you have to import them one package at a time. It's a better practice, though, to only import the classes that you actually need.
Because import some.example.Type; is only to import types not the packages. import some.example.*; means you're importing all the Types contained inside some.example package not the other packages inside it.
This is because import means the code of that file will be available for your program at run time and package itself doesn't contain any code. It contains files which have the code.
That's why you can't import all the built-in code in a single import statement. At max you can import in a single statement is all the code available in different files within a package and you know the way import some.example.*;
As you can read in this link on Oracle docs, under the heading Apparent Hierarchies of Packages:
At first, packages appear to be hierarchical, but they are not. For
example, the Java API includes a java.awt package, a java.awt.color
package, a java.awt.font package, and many others that begin with
java.awt. However, the java.awt.color package, the java.awt.font
package, and other java.awt.xxxx packages are not included in the
java.awt package. The prefix java.awt (the Java Abstract Window
Toolkit) is used for a number of related packages to make the
relationship evident, but not to show inclusion.
Using a wildcard when importing classes may clutter your classes namespace so if you'll have a class named ClassA in more then one import (e.g. import com.example1.* and import com.example2.* where ClassA is defined in both and you need only the implementation in com.example1) you will have a conflict so only import what you really need to use.
Most IDEs allows you to easily organize your imports so only the classes that you really need to use will be imported
It is not like what you think, consider when you want to use ChannelHandler interface you could do either import io.netty.channel.*; or import io.netty.channel.ChannelHandler; but you cant use import io.netty.*; and that why you cant import java.*;
You can import java.util.* it work fine but when you import specific class instead of all class it take less time in importing but if you are importing too many classes from same package you can import all classes using package.* and it takes less time for jvm to fetch instead of taking one by one.
By convention you can use import package.* if you are using too many classes from same package.
Why? Because Java by design is divided into different packages. This way you can't pollute your namespace with all the classes that exist, and you have to actually specify what classes in which namespace you intend to use.
In java, packages don't relate to each other - there is no hierarchy (even if it would look like it), just flat names of packages.
As to how you could circumvent that, I'd think the only way would be to iterate all packages and generate your java code based on that iteration. There is no straight way to do it.

Importing packages from other project in java

I want to import packages that my friend created for his project to my project. What should I do to import that package. Do I have to create library file and include those in my project ?
import <the complete package name>
like
import java.util.LinkedList;
import java.util.Queue;
if you want to do static import than:
import static java.lang.Math.PI;
Do you need something else?
You have to add the external jars in the classpath and then you will be able to import the classes they provide.
import fully.qualified.class.name.from.external.jar.ClassName;
packagee line should be a first line of you program.
if you want load all classes of package the use below line.
import packagename.*;
if you want to import single class of package use
import packagename.classname;

Import issue in Eclipse

I have the following import in my java code in an eclipse project
import org.jscience.*;
However upon writing the code
Real r1 = new Real.valueOf(1);
I receive the error
Real cannot be resolved to a type
I am puzzled as to why this is happening as I am correctly importing (or so I believe)
Also to note, eclipse suggests using
import org.jscience.mathematics.number.Real;
But I am trying to avoid using the import for the full class location within the Jar.
Any ideas?
Thanks in advance
Martin
Try
import org.jscience.mathematics.number.*;
Because
import org.jscience.*;
will actually only import classes from within the jscience package and not any subpackages.
If we suppose your import is not throwing errors, try importing g.jscience.mathematics.number.*.

what is the need for importing libraries multiple times

in most code examples I see people doing this.
import javax.swing.*; // for the frame
import java.awt.*; // for the checkBox and the label
import java.awt.event.*; // for the checkBox listener
If I am correct when we say import java.awt.* it imports everything inside it, so there wont be a need to say import java.awt.event.*; or is there a speed improvement? can anyone also explain what importing a library does, is it importing a simple text class to be included in the source or telling jvm to include the byte code of whatever is imported? so importing in java does nothing but switch the namespace, so I dont have to type long class names?
Forget the term subpackage. Do it quick. It does not exist in java world.
java.awt is a package (namespace), java.awt.event is another one and they have nothing in common. Their names share some characters, but the packages are totally unrelated. The import statements imports a class or some classes from exactly one package (namespace). If you need classes from a different package (namespace), then you have to add another import statement.
BTW, in response to a comment to another answer: You do not have to use import statements. If you don't use them, you simply have to use the fully qualified classnames in your java source file (except: classes from java.lang and the current package are imported automatically). So import could be considered as a convenient way to keep the code readable.
Importing is not required in order to use a class in your source file.
The line...
import java.awt.*;
...doesn't mean that all subpackages will also be imported. You have to explicitly import every package. As an example, importing java.* doesn't give you the entire java library.
For what it's worth, I recommend importing the specific classes only unless you have good reason to use *.
Importing a package doesn't import its subpackages.
Importing is about switching the namespace, too.
If you only had import java.awt.* and you were to use the class java.awt.Outer.Inner, everywhere in your code you have to refer to it as Outer.Inner.
By contrast, when you say import java.awt.Outer.*, you can refer to the inner class as just Inner.
Importing is just a compile time feature, in bytecode you will find only direct references to specific classes, everytime its instance is used.
"import" constructs exist just to eliminate use of full class name everytime.

Categories

Resources