Title: java.lang.System Example - Java Code Geeks
Open Graph Title: java.lang.System Example - Java Code Geeks
Description: In this tutorial we will discuss about the System class in Java. It contains a number of useful fields, such as the standard input, the standard output
Open Graph Description: In this tutorial we will discuss about the System class in Java. It contains a number of useful fields, such as the standard input, the standard output
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/"},"author":{"name":"Sotirios-Efstathios Maneas","@id":"https://examples.javacodegeeks.com/#/schema/person/03673b064b32807176478bd144079be1"},"headline":"java.lang.System Example","datePublished":"2014-09-10T12:00:52+00:00","dateModified":"2014-11-09T21:03:13+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/"},"wordCount":389,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["System"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/","url":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/","name":"java.lang.System Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-09-10T12:00:52+00:00","dateModified":"2014-11-09T21:03:13+00:00","description":"In this tutorial we will discuss about the System class in Java. It contains a number of useful fields, such as the standard input, the standard output","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-development/core-java/lang/system/java-lang-system-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"lang","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/"},{"@type":"ListItem","position":5,"name":"System","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/system/"},{"@type":"ListItem","position":6,"name":"java.lang.System Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/03673b064b32807176478bd144079be1","name":"Sotirios-Efstathios Maneas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Stathis-Maneas-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Stathis-Maneas-96x96.jpg","caption":"Sotirios-Efstathios Maneas"},"description":"Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.","sameAs":["https://www.cs.utoronto.ca/~smaneas/","https://www.facebook.com/stathis.maneas","https://linkedin.com/in/smaneas"],"url":"https://examples.javacodegeeks.com/author/stathis-maneas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"lang","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/"}},{"@type":"ListItem","position":5,"item":{"name":"System","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/system\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-09-10T15:00:52+03:00","datePublished":"2014-09-10T15:00:52+03:00","dateModified":"2014-11-09T23:03:13+02:00","headline":"java.lang.System Example","name":"java.lang.System Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/lang\/system\/java-lang-system-example\/","description":"In this tutorial we will discuss about the System class in Java. It contains a number of useful fields, such as the standard input, the standard output and the standard error streams. The System class","copyrightYear":"2014","articleSection":"System","articleBody":"In this tutorial we will discuss about the System class in Java. It contains a number of useful fields, such as the standard input, the standard output and the standard error streams. The System class is declared as final and thus, it cannot be instantiated.\r\n\r\nAlso, the System class contains a large number of methods that can be used to retrieve external defined properties and environmental variables, load files and libraries, etc. Also, the System class contains a sample method for copying a portion of an array to another array.\r\n\r\nFinally, the System class exists since the 1.0 version of Java.\r\n\r\nFields\r\n\r\n\tSystem.in: The standard input stream.\r\n\tSystem.out: The standard output stream.\r\n\tSystem.err: The standard error stream.\r\n\r\n\r\nImportant: All these streams are already open and ready to accept data.\r\n\r\nArrayCopyExample.java:\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\n\r\npublic class ReadLineExample {\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\tBufferedReader rd = new BufferedReader(new InputStreamReader(System.in));\r\n\t\t\r\n\t\t\/\/Read a line from the standard input.\r\n\t\tString inputLine = rd.readLine();\r\n\t\t\r\n\t\t\/\/ Reverse the string.\r\n\t\tStringBuilder builder = new StringBuilder(inputLine);\r\n\t\tbuilder.reverse();\r\n\t\t\r\n\t\tSystem.out.println(\"Input string: \" + inputLine);\r\n\t\tSystem.out.println(\"Reversed string: \" + builder.toString());\r\n\t\tSystem.err.println(\"Reversed string: \" + builder.toString());\r\n\t\t\r\n\t\t\/\/ Close the stream.\r\n\t\trd.close();\r\n\t}\r\n}\r\n\r\n\r\nIn this example we read a complete line from the standard input stream and we write that line in reversed order in both the standard output and standard error streams. A sample execution is shown below:\r\n\r\nInput string: Hello from Java Code Geeks!\r\nReversed string: !skeeG edoC avaJ morf olleH\r\nReversed string: !skeeG edoC avaJ morf olleH\r\n\r\n\r\nArray Copy\r\nThe System class provides a general method for copying a portion of an array into another array. The method's declaration is the following:\r\n\r\n\tpublic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)\r\n\r\n\r\nLet's explain each parameter separately:\r\n\r\n\tsrc: the source array.\r\n\tsrcPos: the position in the source array from which, the copy procedure begins.\r\n\tdest: the destination array.\r\n\tdestPos: the position in the destination array where data will be copied.\r\n\tlength: the number of elements to be copied.\r\n\r\n\r\nArrayCopyExample.java:\r\n\r\npublic class ArrayCopyExample {\r\n\tprivate final static int TOTAL_ELEMENTS = 10;\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tint[] src = new int[TOTAL_ELEMENTS];\r\n\t\t\r\n\t\t\/\/ Populate the array with some elements.\r\n\t\tfor(int i = 0; i < TOTAL_ELEMENTS; ++i)\r\n\t\t\tsrc[i] = i + 1;\r\n\r\n\t\t\/\/ Print the elements.\r\n\t\tSystem.out.print("Source array: ");\r\n\t\tfor(int i = 0; i < TOTAL_ELEMENTS; ++i)\r\n\t\t\tSystem.out.print(src[i] + " ");\r\n\t\tSystem.out.println();\r\n\t\t\r\n\t\t\/\/ Copy the array to the destination.\r\n\t\tint dst_size = src.length \/ 2;\r\n\t\tint[] dst = new int[dst_size];\r\n\t\tSystem.arraycopy(src, 0, dst, 0, dst_size);\r\n\t\t\r\n\t\t\/\/ Print the elements.\r\n\t\tSystem.out.print("Destination array: ");\r\n\t\tfor(int i = 0; i < dst_size; ++i)\r\n\t\t\tSystem.out.print(dst[i] + " ");\r\n\t\tSystem.out.println();\r\n\t}\r\n}\r\n\r\n\r\nIn this example, we copy the first half of the src array, to the dst array.\r\n\r\nA sample execution is shown below:\r\n\r\nSource array: 1 2 3 4 5 6 7 8 9 10 \r\nDestination array: 1 2 3 4 5 \r\n\r\n\r\nSystem Properties\r\nThe System class contains a private instance of the Properties class, which is used to provide the configuration of the current working environment. \r\n\r\nImportant: A table containing the most important system properties can be found here.\r\n\r\nIn the following example, we print a number of system properties to the console:\r\n\r\nSystemPropertiesExample.java:\r\n\r\npublic class SystemPropertiesExample {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ Prints the version of Java.\r\n\t\tSystem.out.println(\"Java version: \" + System.getProperty(\"java.version\"));\r\n\r\n\t\t\/\/ Prints the version of the underlying operating system.\r\n\t\tSystem.out.println(\"OS version: \" + System.getProperty(\"os.version\"));\r\n\r\n\t\t\/\/ Prints the user's home directory.\r\n\t\tSystem.out.println(\"Home directory: \" + System.getProperty(\"user.home\"));\r\n\t}\r\n}\r\n\r\n\r\nA sample execution is shown below:\r\n\r\nJava version: 1.8.0_20\r\nOS version: 3.13.0-24-generic\r\nHome directory: \/home\/stathis\r\n\r\n\r\nEnvironmental variables\r\nThe System class also provides a method, called getenv(), which returns all defined environmental variables:\r\n\r\nEnvironmentalVariablesExample.java:\r\n\r\nimport java.util.Map;\r\n\r\npublic class EnvironmentalVariablesExample {\r\n\tpublic static void main(String[] args) {\r\n\t\tMap vars = System.getenv();\r\n\t\t\r\n\t\t\/\/ Print all defined environmental variables.\r\n\t\tfor(String key: vars.keySet())\r\n\t\t\tSystem.out.println(\"Key: \" + key + \", Value: \" + vars.get(key));\r\n\t}\r\n}\r\n\r\n\r\nA sample execution is shown below:\r\n\r\nKey: PATH, Value: \/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games\r\nKey: LC_MEASUREMENT, Value: el_GR.UTF-8\r\n.........\r\nKey: USER, Value: stathis\r\nKey: HOME, Value: \/home\/stathis\r\n\r\n\r\nCurrent time\r\nThe System class contains a sample method, called currentTimeMillis(), which returns the current time in milliseconds. The value returned by this method is defined as the difference between the current time and the midnight of January 1st, 1970 UTC.\r\n\r\nA sample invocation is shown below:\r\n\r\nSystem.out.println(\"The current time in milliseconds: \" + System.currentTimeMillis());\r\n\r\n\r\nDownloadYou can download the full source code of this example here: SystemClassExample.zip.","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/lang\/system\/java-lang-system-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Sotirios-Efstathios Maneas","url":"https:\/\/examples.javacodegeeks.com\/author\/stathis-maneas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:author | https://www.facebook.com/stathis.maneas |
| article:published_time | 2014-09-10T12:00:52+00:00 |
| article:modified_time | 2014-11-09T21:03:13+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Sotirios-Efstathios Maneas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Sotirios-Efstathios Maneas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1