Friday, March 29, 2024
HomeInterview QuestionsPackages in Java

Packages in Java

-

Hello World! Today’s blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with syntax and examples.

Definition For Packages In Java

Packages in java are collection of classes, interfaces (or) other packages. They are used for organizing java project. The primary purpose of using packages is to avoid naming conflicts and make it easier to manage code.

When we are creating a package, the name should be written in camelCase style.

To access the package from different packages, you need to use the “import” keyword:

Syntax For Importing Packages In Java

We can import packages from another packages in three different types:

Type 1: Importing A Certain Class Only

import java.util.Test; // if you want only certain class we can import like this.

Example for Type 1

import java.lang.Test;
class Test
{
public static void main (String args[])
{
System.out.println("Hello World")
}
}

Related Topics:

Check Array Formation Through Concatenation | Leetcode Problem 1640

Type 2: Accessing Any Class Within This Package

import java.lang.*; // In this type “*” means we can use any class from this package

Example for type 2

import java.lang.*;
class Mypack
{
public static void main (String args[])
{
System.out.println(“Hello World”);
}
}

Type 3: Directly Create Object Without Importing Any Package

Here we are not importing any kind of packages. We will write it directly when we are creating an object.

Example for type 3

class Test
{
public static void main ( String args[] )
{
java.lang.String Str = new java.lang.String("hello");
}
}

So that’s it for todays topic, we will reconnect with another interesting topic.

If there are any queries in this topic please let me know in the comments.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

House Robber Problem | Maximum Sum of non-adjacent elements

In this blog post, we will discuss about how to find maximum sum of Non-Adjacent elements. Since House Robber Problem is a typical DP Problem...

Binary Tree Traversals in Kotlin – PreOrder, InOrder and PostOrder Traversals

Binary Tree Traversals are most common Tree Problems in an Interview as well as Competitive Programming. They can be efficient while searching, inserting and deleting...

Building a Multi Module App in Android | Modularization in Android #1

Recently, I was in requirement for developing a Multi Module app in Android and I was going through this great lecture on it. This article...

Python Lambda Function with Examples

In this article, we will discuss briefly about Python Lambda function along with an example. A Lambda function is an anonymous function which takes arguments and...

Follow us

1,358FansLike
10FollowersFollow
399SubscribersSubscribe

Most Popular