Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use c/cpp code in java??

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a question, although cpp is derived from c so we can use c code in cpp, and java is also somehow derived from cpp, so can we use cpp and c code in java, if yes then how, any example??
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like Java Native Interface.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:i have a question, although cpp is derived from c so we can use c code in cpp, and java is also somehow derived from cpp, so can we use cpp and c code in java


No. Java is not as close to C++ as C is.

If you want to use native libraries written in C/C++ from your Java code, then you can google for JNI tutorial or JNA tutorial.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i think the concept of oops are inherited from cpp??
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also can you tell me advantages of using native libraries in java??
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
No. Java is not as close to C++ as C is.

If you want to use native libraries written in C/C++ from your Java code, then you can google for JNI tutorial or JNA tutorial.



Agreed. "Java and C" is nowhere near as close as "C++ and C" is.... C++ actually allows the embedding of C code, where the C++ compiler will compile the C code. It also allows C++ code to directly link with C libraries unchanged. Java doesn't have such integration.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:but i think the concept of oops are inherited from cpp??


First of all, no. OOP (not "oops") existed before C++.

Second, so what? Just because some language derives certain high-level concepts from another language, that's no reason to think their syntaxes will have any similarity at all.

Okay, fine, you can write a little C++ code in Java:


That is legal C++ code, and it's also legal in Java.

Enjoy!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Jain wrote:also can you tell me advantages of using native libraries in java??


The main reason to use them is to get access to lower-level or system-dependent things that's aren't available in core Java.

I suppose in theory there might be cases where a native library will perform better or use less memory than its Java equivalent, but I don't know how often they're used for that reason.'

However, if by "use C/C++ in Java" you mean it's because you want to use C++'s syntax, then just write in C++ and forget about Java.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic