Define type data in java

Define type data in java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Java User-defined Data Types

Java User-defined Data Types

Java User-defined data types are the customized data types created by the developers by exploiting the special features offered by the Java language. This customized data type combines data types of a group of data elements with homogenous or assorted data types. It utilizes the object-oriented functionalities of Java and allows creation of any kind of data types as per the need.

Web development, programming languages, Software testing & others

These data types have versatile characteristics and behavior and it provides greater flexibility to developers in improving their productivity and maintainability of the programs. Let’s go through features of these special data types in this article.

Data types of all the variables should be declared well ahead of its compilation and it should be one from the standard type that Java offers. The variables cannot hold any other data type than what was declared. But the user-defined data type provides the flexibility to define the data type at a group level.

User-defined Data Types in Java

Two major User defined data types are:

1. Class

Java a true object-oriented language is full of Classes that encapsulate everything from data elements that acts as instance variables and functions to process the data. It also provides templates to create Objects that are instances of Class that contain a method for performing the defined functions and interact with other parts of the program.

A class typically contains

Invoking variables with data types

How Objects are created?

Objects are created or instantiated from the class and used as individual instance of the class. Instance variables are created for each of the objects to interact with other components of the program and there could be as many objects created and each object will have

  • Clear identity with a unique name
  • Different set of instance variables to reflect its state.
  • All the functionalities that define its behavior

Each class is a user-defined data type with the combined effect of all the data types of its data elements and each object is a variable data.

Example #1: Class Definition

// Sample program to define Class, Object and Method class ValueStore //Class definition with default access < int Rate; // Variables with standard data types int Qty; void AssignData(int A, int B) // method definition < Rate = A; // Assigning the values Qty = B; >int ComputeValue() // Another method to compute value < int value; // variable definition value = Rate * Qty; return(value); // Returning the value >>

Class with name ValueStore with default access is defined. It has two instance variables Rate, QTY used in the method interactions. Two methods AssignData, ComputeValue with respective function codes are created inside the class.

public class OrderValueCompute // Public class < public static void main(String[] args) // Execution starts here < int value1; // variable definition int value2; ValueStore VS1; // Object VS1 definition VS1 = new ValueStore(); // Object VS1 instantiated ValueStore VS2; // Object VS2 definition VS2 = new ValueStore(); // Object VS2 instantiated VS1.AssignData (200,10); // Assigndata method in Object VS1 invoked //with values value1 = VS1.ComputeValue(); // Computedata method in object VS1 // invoked VS2.AssignData (500,20); // Assigndata method in Object VS2 // invoked with values value2 = VS2.ComputeValue(); // Computedata method in object VS2 //invoked System.out.println("Order value 1 " +value1); // Output 1 displayed System.out.println("Order Value 2 " +value2); // Output 2 displayed >>

In the execution class, the objects in the other class are defined, instantiated. Methods are invoked. Results obtained and displayed. Class is a user-defined data type and each object is a variable in it.

Java User-defined Data Types 1

2. Interface

Interface is similar to Class in architecture. It has data variables and methods. It broadly suggests what the classes calling it should do but it does not suggest how it should be carried out.

The method in an interface differs from a normal method in a class by

  • It is not full-fledged
  • It is only an abstract and it has the only a definition and the body is empty.
  • It will not perform any functionalities as such

Interfaces cannot instantiate any objects like class. But it facilitates abstraction, Multiple inheritances, and loose coupling of classes.

Class extends to another super class by a level. Interface extends to another super interface by a level. Class implements interface and multiple inheritance is achieved indirectly which is otherwise not possible in a class.

Each interface created by a user has a unique data type with a combined effect of its data elements. Objects created in these classes uses interfaces also and each of these objects are the variables for interface data types.

Below example shows how classes implement interfaces, how objects of these classes are created linking the interfaces, and how they are executed.

// Sample program to demonstrate Interfaces interface Intface //Interface is defined < // It has its own data type public void dayname(); //Abstract method within the interface >class day1 implements Intface < // Class interacts through public void dayname() // interface implementation < // (each class is a variable for interface) System.out.println("Monday"); >> class day2 implements Intface < // Another class public void dayname() < System.out.println("Tuesday"); >> class day3 implements Intface < // Another class public void dayname() < System.out.println("Wednesday"); >> public class Subject < // Execution starts here public static void main(String[] args) < Intface t = new day1(); // Object of day1 class thru interface t.dayname(); // method invoked Intface tx = new day2(); // Object of day2 class thru interface tx.dayname(); Intface tx2 = new day3(); // Object of day3 class thru interface tx2.dayname(); >> // objects t, tx1, tx2 are the variables of // the interface data type

Java User-defined Data Types 2

Conclusion

Class and interfaces are the major user-defined data types. Some forums have the opinion that String and Arrays are also part of user-defined types. Strictly speaking, they are part of standard data types of Java and hence they are not discussed.

This is a guide to Java User-Defined Exception. Here we discuss the Introduction, syntax, How Objects are created? examples with code implementation. You may also have a look at the following articles to learn more –

89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8

Источник

User defined data types

User defined data types are those which are developed by programmers by making use of appropriate features of the language.

User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in java it is achieved using class concept.

Note: In java both derived and user defined data type combined name as reference data type.

In C language, user defined data types can be developed by using struct, union, enum etc. In java programming user defined datatype can be developed by using the features of classes and interfaces.

Example

In java we have eight data type which are organized in four groups. They are

  • Integer category data types
  • Character category data types
  • Float category data types
  • Boolean category data types

Integer category data types

These category data types are used for storing integer data in the main memory of computer by allocating sufficient amount of memory space.

Integer category data types are divided into four types which are given in following table

Data Type Size Range
Byte 1 + 127 to -128
Short 2 + 32767 to -32768
Int 4 + x to — (x+1)
Long 8 + y to — (y+1)

Character category data types

A character is an identifier which is enclosed within single quotes. In java to represent character data, we use a data type called char. This data type takes two byte since it follows Unicode character set.

Data Type Size(Byte) Range
Char 2 232767 to -32768

Why Java take 2 byte of memory for store character ?

Java support more than 18 international languages so java take 2 byte for characters, because for 18 international language 1 byte of memory is not sufficient for storing all characters and symbols present in 18 languages. Java supports Unicode but c support ascii code. In ascii code only English language are present, so for storing all English latter and symbols 1 byte is sufficient. Unicode character set is one which contains all the characters which are available in 18 international languages and it contains 65536 characters

Float category data types

Float category data type are used for representing float values. This category contains two data types, they are in the given table

Data Type Size Range Number of decimal places
Float 4 byte +2147483647 to -2147483648 8
Double 8 byte + 9.223*1018 16

Boolean category data types

Boolean category data type is used for representing or storing logical values is true or false. In java programming to represent Boolean values or logical values, we use a data type called Boolean.

Why Boolean data types take zero byte of memory ?

Boolean data type takes zero bytes of main memory space because Boolean data type of java implemented by Sun Micro System with a concept of flip — flop. A flip — flop is a general purpose register which stores one bit of information (one true and zero false).

Note: In C, C++ (Turbo) Boolean data type is not available for representing true false values but a true value can be treated as non-zero value and false values can be represented by zero

Источник

Читайте также:  What is listener class in java
Оцените статью