![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C/C++ Pointers vs Java references\n
Pointers
In C, C++ programming languages, a pointer is a variable that holds the address of another variable.
example
#include <iostream> using namespace std; int main() { //int variable int i = 8; //pointer variable int * pI; //assign the address of i to its pointer pI = &i; //print the number cout<<i<<endl; //print the address of the number cout<<pI<<endl; //print the value pointed by pointer count<<*pI<<endl; //change the value of variable using its pointer *pI = 10; //print the number cout<<i<<endl; }
Output
8 0x7fee1ae7bc94 8 10
References
In java programming languages, a reference is a variable that refers to an object and using which we can utilize the properties and functions of an object.
example
public class Tester { public static void main(String[] args) { Student student = new Student(); student.setName("Mahesh"); System.out.println(student.getName()); } } class Student { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Output
Mahesh
Differences between Pointers and References
Following are some of the differences between C/C++ Pointers and References.
No Pointer Arithmetic in Java. Pointers are memory addresses and a pointer points to a memory address of a variable. In C/C++, a pointer can be incremented/decremented to point to a new address but in Java, arithmetic operations on references are not allowed.
No Pointer Manipulation in Java Although a reference internally uses a pointer but Java does not allow any manipulation to an underlying pointer using a reference variable. It makes java more secure and robust. A reference can refer to an object or be null only.
No Casting of pointers in Java In C/C++, we can cast int* to char* but in Java, only related objects can be cast e.g. object of the same hierarchy.