File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to use c/cpp code in java?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to use c/cpp code in java??" Watch "how to use c/cpp code in java??" New topic
Author

how to use c/cpp code in java??

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
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

Joined: Feb 07, 2012
Posts: 344

Sounds like Java Native Interface.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
but i think the concept of oops are inherited from cpp??
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
also can you tell me advantages of using native libraries in java??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay thanks....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use c/cpp code in java??