This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Java Program to convert all primitives into its corresponding | |
| //wrapper objects and vice-versa | |
| public class WrapperExample3{ | |
| public static void main(String args[]){ | |
| byte b=10; | |
| short s=20; | |
| int i=30; | |
| long l=40; | |
| float f=50.0F; | |
| double d=60.0D; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Java program to convert object into primitives | |
| //Unboxing example of Integer to int | |
| public class WrapperExample2{ | |
| public static void main(String args[]){ | |
| //Converting Integer to int | |
| Integer a=new Integer(3); | |
| int i=a.intValue();//converting Integer to int explicitly | |
| int j=a;//unboxing, now compiler will write a.intValue() internally | |
| System.out.println(a+" "+i+" "+j); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Java program to convert primitive into objects | |
| //Autoboxing example of int to Integer | |
| public class WrapperExample1{ | |
| public static void main(String args[]){ | |
| //Converting int into Integer | |
| int a=20; | |
| Integer i=Integer.valueOf(a);//converting int into Integer explicitly | |
| Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally | |
| System.out.println(a+" "+i+" "+j); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Java class to test the encapsulated class Account. | |
| public class TestEncapsulation { | |
| public static void main(String[] args) { | |
| //creating instance of Account class | |
| Account acc=new Account(); | |
| //setting values through setter methods | |
| acc.setAcc_no(7560504000L); | |
| acc.setName("Sonoo Jaiswal"); | |
| acc.setEmail("sonoojaiswal@javatpoint.com"); | |
| acc.setAmount(500000f); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Account class which is a fully encapsulated class. | |
| //It has a private data member and getter and setter methods. | |
| class Account { | |
| //private data members | |
| private long acc_no; | |
| private String name,email; | |
| private float amount; | |
| //public getter and setter methods | |
| public long getAcc_no() { | |
| return acc_no; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Java class which has only setter methods. | |
| public class Student{ | |
| //private data member | |
| private String college; | |
| //getter method for college | |
| public void setCollege(String college){ | |
| this.college=college; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Java class which has only getter methods. | |
| public class Student{ | |
| //private data member | |
| private String college="AKG"; | |
| //getter method for college | |
| public String getCollege(){ | |
| return college; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Java class to test the encapsulated class. | |
| package com.javatpoint; | |
| class Test{ | |
| public static void main(String[] args){ | |
| //creating instance of the encapsulated class | |
| Student s=new Student(); | |
| //setting value in the name member | |
| s.setName("vijay"); | |
| //getting value of the name member | |
| System.out.println(s.getName()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A Java class which is a fully encapsulated class. | |
| //It has a private data member and getter and setter methods. | |
| package com.javatpoint; | |
| public class Student{ | |
| //private data member | |
| private String name; | |
| //getter method for name | |
| public String getName(){ | |
| return name; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| struct Person | |
| { | |
| char name[50]; | |
| int age; | |
| float salary; | |
| }; |
NewerOlder