www.fatihkabakci.com

Personal Website and Computer Science TUR EN

JAVA AUTO BOXING ve UNBOXING

Last update: 10/3/2014 6:27:00 PM

Yazan:Fatih KABAKCI

Javanın JDK 5.0 sürümü ile geliştirilen kutulama özelliği,artık daha sade bir kullanımla programcılara sunulmuştur.Javanın otomatik kutulama özelliği ile,sarmalanan primitif veriler,harici metotların kullanımına gerek kalmadan,Integer,Double gibi nesneler ile esnek bir şekilde çalışmaktadır.Javanın çok öncesinden beri,primitif tipleri nesneleştirme benzetimiyle sarmalayan boxing ve unboxing özelliği,JDK 5 ile minimum hata ve yöntemle otomatikleştirilmiştir.
Bu otomatikleştirme işlemi basitçe,bir değişkene değer atamak kadar kolaylaştırılmıştır.Daha önceden anlatılan bir takım kurucular ve tipe göre değer döndüren metotların kullanılma zorunluluğu ortadan kalkmıştır.Örnek olarak,
public class Wrappers
public static void main(String args[])
{
int value=3;
Integer wrap1=value; // boxing
wrap1++;  // unboxing,boxing
value=wrap1; unboxing
}
şeklinde programlar yazılabilir.Yukarıdaki programda öncelikle primitif bir int tanımlanıyor.Bu değer bir Integer nesnesi tarafından sarmalanarak auto-boxing işlemine tabi tutuluyor.Daha sonra dikkat edilirse,bir nesne olan wrap1,öncelikle auto-unboxing işlemine tabi tutuluyor,akabinde 1 arttırılarak tekrardan auto-boxing işlemine tutuluyor.Son olarak ise value primitifine atanarak saf veri halini alıyor.

Yukarıdaki programda dikkat edilirse,nesne ve saf veriler sanki eş tipli yapılarmış gibi birbirlerine atanabilmektedir.Aslında arka planda gelişen olay,bilindiği gibi Java çalışma zamanı sisteminin veriler üzerinde yaptığı boxing ve unboxing işlemleridir.Bu durum sadece yapılan bir takım hataları önler ve programcıya zaman kazandırır.

Daha önceden anlatılan boxing ve unboxing yönteminde toplama örneği verilmişti.

Bu örneğin modern yaklaşımla yazılmış hali ise aşağıdaki gibi olacaktır.

Bu şekliyle yukarıdaki kod,daha sade,daha güvenli ve daha moderndir.Ancak unutulmamalıdır ki javanın kutulama özelliğinin hangi yöntemle yapılırsa yapılsın,tasarımsal olarak gerçekten gereken durumlarda uygulanması gerektiğidir.Bir primitif verinizi,nesneleştirmek istediğiniz zamanlarda,javanın auto boxing özelliği size yardımcı olacaktır.

İlişkili yazılar:
Java Boxing ve Unboxing
Primitif Tipler
There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.