• Post Reply Bookmark Topic Watch Topic
  • New Topic

platform independent vs portability?  RSS feed

 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone clear the difference between these 2 concepts with suitable example
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Platform independent in the case of Java means that Java programs are completely independent of the operating system on which you run them. When you write a Java program, it can run on Windows, Linux, OS X or any other operating system without any changes or need to recompile the code (as long as there's a Java runtime environment available for the operating system).

Portable means that you can recompile the source code of a program to a platform-specific executable without changing the source code itself; you just need to compile it. The resulting executable will only work on the specific CPU type and operating system that you have compiled it for.

If you write a program in for example C++ and you use libraries and APIs that are for a specific operating system, then your program is not portable. You can't easily create a version of your program that would run on an Apple Mac, for example, because the Windows API that you used doesn't exist there.

Also in Java it's possible to write programs that are not platform independent. For example if you use paths in your code like "C:\Temp" etc. then it's not going to work on OS X or Linux, because path names look different on those operating systems.
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the commonest ways to lose platform independence is to hardcode line end characters. Never mind how many books use \n, it is almost always incorrect bcause line ends are platform‑specific.
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I usually prefer to use String.format() with the "%n" conversion.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this definition of portability in context of java is incorrect :"compiled Java language programs are portable to any system on which the Java interpreter and run-time system have been implemented"
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is true in a very general sense, but as others have already pointed out it is entirely possible to write platform-dependent programs in Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!